Attempting to input information into a form and then showcase the data both on the current page and in a separate pop-up window

Each time I complete the entire form, the data vanishes. However, if I only input three fields, it displays properly. But as soon as I enter the last piece of data and click on display, everything gets erased. Moreover, I also require the data to be shown in a popup window. I believe I have been looking at the code for too long.

function displayData() {

  var a = document.getElementById("name").value;
  var b = document.getElementById("add").value;
  var c = document.getElementById("phone").value;
  var d = document.getElementById("course").value;
  var e = document.getElementById("email").value;

  data1.innerHTML = ("<ul><li><strong>Name:</strong>" + a + "</li>" +
    "<li><strong>Address:</strong>" + b + "</li>" +
    "<li><strong>Phone:</strong>" + c + "</li>" +
    "<li><strong>Courses:</strong>" + d + "</li>" +
    "<li><strong>Email:</strong>" + e + "</li></ul>");

}
<form>

  <p align="center">

    <lable><b>Name:</b></lable>
    <input type="text" id="name" required/>
    <br>

  </p>
  <p align="center">

    <lable><b>Address:</b></lable>
    <input type="text" id="add" required/>
    <br>

  </p>
  <p align="center">

    <lable><b>Phone:</b></lable>
    <input type="tel" id="phone" required/>
    <br>

  </p>
  <p align="center">

    <lable><b>Email:</b></lable>
    <input type="text" id="email" required/>
    <br>

  </p>
  <p align="center">

    <select class="scrol" id="course" size="3" width="15px">

      <option>Android</option>
      <option>Java</option>
      <option>C#</option>
      <option>Data Base</option>

    </select>

  </p>

  <br>

  <p align="center">

    <button type="submit" onclick="displayData()">Display</button>
    <button type="reset">Reset</button>

  </p>

</form>

Message is:

<p id="data1"></p>

The output works fine until all fields are filled in. Any help would be greatly appreciated

Answer №1

How can I stop a form from being submitted?

function showData(event) {
  event.preventDefault();
  var name = document.getElementById("name").value;
  var address = document.getElementById("add").value;
  var phone = document.getElementById("phone").value;
  var course = document.getElementById("course").value;
  var email = document.getElementById("email").value;
  var message = document.getElementById("data1");

  data1.innerHTML = ("<ul><li><strong>Name:</strong>" + name + "</li>" +
    "<li><strong>Address:</strong>" + address + "</li>" +
    "<li><strong>Phone:</strong>" + phone + "</li>" +
    "<li><strong>Courses:</strong>" + course + "</li>" +
    "<li><strong>Email:</strong>" + email + "</li></ul>");
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />
<form>
  <label>Name:
  <input type="text" id="name" required/></label>
  <label>Address:
  <input type="text" id="add" required/></label>
  <label>Phone:
  <input type="tel" id="phone" required/></label>
  <label>Email:
  <input type="text" id="email" required/></label>
  <select id="course">
    <option>Android</option>
    <option>Java</option>
    <option>C#</option>
    <option>Data Base</option>
  </select>
  <button onclick="showData(event)">Display</button>
  <button type="reset">Reset</button>
</form>

Message is:

<p id="data1"></p>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Employ Angular within the parameters of a JavaScript function call

My goal is to incorporate Google Maps into my webpage for various locations (lieux). Within the HTML page, I have the necessary function set up for Google Maps. <script> function initialize(long,lat) { var mapCanvas = document.getElementById( ...

Retrieving relevant information using an API and displaying it in components with Next.js

I am currently working with Notion's API to retrieve a list of Places along with their corresponding Cities, and then displaying this information through various components. As someone who is relatively new to the world of JavaScript (v2023), I am fac ...

Tips for connecting a webpage element with endless scrolling functionality

Currently, I am dealing with a webpage that features a container utilizing infinite scroll to load numerous articles. I am faced with the challenge of how to effectively link to a particular article when its corresponding div is only loaded into the DOM u ...

Problem with implementing swipeable tabs using Material-UI in React

I'm experiencing an issue in my application with the react swipeable tabs from material-ui. I followed all the installation steps recommended in the documentation. Currently, I am encountering the error shown in the screenshot below. Could there be a ...

Hide the modal pop up once the message has been submitted on the server side

I'm working with a bootstrap modal pop up that contains various controls and a submit button. After successfully submitting, I need to close the pop-up. The code snippet below displays the message "Record Saved successfully." if (strMessage == "Succe ...

What are some effective methods to completely restrict cursor movement within a contenteditable div, regardless of any text insertion through JavaScript?

Recently, I encountered the following code snippet: document.getElementById("myDiv").addEventListener("keydown", function (e){ if (e.keyCode == 8) { this.innerHTML += "&#10240;".repeat(4); e.preventDefault(); } //moves cursor } ...

Having trouble calculating the sum of two numbers in JavaScript?

I recently encountered an issue in my code where I had a number array and was trying to display the sum in a text field. However, whenever I added a new number to the array and tried to calculate the sum again, it would concatenate instead of adding up pro ...

What is the best way to assess a scope object within an ng-Class directive?

Whenever I click a button, it moves to the next item in an array. Once it reaches the last item in the array, I want to assign the "endButton" class to the button tag. I plan to use the ng-class directive within the HTML code to evaluate the expression. T ...

When certain triggers are activated, a hidden textbox revealed through javascript is made visible

After changing a dropdown value (from ddlSource) and hiding some text boxes using JavaScript, everything works fine. However, when the user enters a certain value in another textbox triggering an AJAX call to populate some labels, upon form reload, the hid ...

Ways to resolve the responseJSON showing {error: "invalid_request", error_description: "Code parameter is missing"}

My current issue involves uploading files to Google Drive from my Vuejs and JavaScript web application. The request I send is returning an error. Upon checking, I noticed that the variable code is null, and even the variable window.location.search is empty ...

Steps to toggle text color upon clicking and switch the color of another text

As a beginner with no Js knowledge, I am trying to achieve a specific function. I want to be able to change the color of text when it is clicked, but also have the previously clicked text return to its original color. <div class="mt-5 fi ...

How to convert an array of keys and an array of values into an array of objects using JavaScript

My question is similar to the one found here: Merging keys array and values array into an object using JavaScript However, I am unable to find a solution for my specific scenario. If I have these two arrays: const keys = ['x', 'y', &ap ...

Ways to avoid browser refresh when uploading files in React applications

I'm working with a simple file upload form in React using hooks. import React, { useState } from 'react'; import { FlexContainer } from '@styles/FlexContainer'; const TestUpload = () => { const [file, setFile] = useState<F ...

What is the best way to apply a jQuery function to multiple div elements simultaneously?

I am looking to implement a show/hide feature for a <div> using JavaScript and DOM properties. The idea is to use JavaScript's onclick() function with two buttons, show and hide, each in their respective <div>. Here is how it would ideall ...

A step-by-step guide on transferring Data URI from canvas to Google Sheet using the fetch method

I am trying to send an image as base64 code to a Google sheet using fetch. However, I am encountering an error that says "data_sent is not defined" when I run my code. I need help identifying the problem and finding a solution to fix it. For reference, & ...

javascript/jquery concatenate all hidden input values into a variable

I am working with a collection of tags that are each added to a hidden input labeled "item[tags][]". <input type="hidden" style="display:none;" value="first" name="item[tags][]"> <input type="hidden" style="display:none;" value="second" name="ite ...

Accessing variables within a JSON string using the Request module in the Firefox Add-on SDK

I am facing an issue with my PHP script on the server. The script retrieves variables from the database and returns a JSON string containing image URLs. Here is a sample of the JSON string: [ { 'src':'imageurl1'} , { 'src':&a ...

The JOI validation process is failing to return all error messages, even though the "abort early" option

I have been encountering an issue while trying to validate my payload using a joi schema. Instead of returning the errors specified in the schema, only one error is being displayed. Even when I provide a payload with name as "int", it only shows one custom ...

Guide on creating a similar encryption function in Node JS that is equivalent to the one written in Java

In Java, there is a function used for encryption. public static String encryptionFunction(String fieldValue, String pemFileLocation) { try { // Read key from file String strKeyPEM = ""; BufferedReader br = new Buffer ...

"Enabling image uploads in Vue.js with TinyMCE: A step-by-step guide

I have integrated tinymce into my Vue.js application, but I am experiencing issues with uploading images. I have included the following package in my project: import Editor from '@tinymce/tinymce-vue'. I believe I might be missing a necessary pl ...