JavaScript Trouble with innerHTML using Javascript

I am making a form with some fields that a user has to fill out. Once he/she fills it out, hits save, the form disappears and a new div appears with what the user filled out. Unfortunately, I cannot get the data to show in this second div that appears. I am not sure what I'm doing wrong, maybe you can help me? Also, I need to use pure Javascript, not jQuery or anything else.

var firstName = document.getElementById('fname').value;
var lastName = document.getElementById('lname').value;
var state = document.getElementById('select_state');
var whichState = state.options[state.selectedIndex].text;
var parname = document.getElementById('pname');
var form = document.getElementById('form_div');
var edit = document.getElementById('view_div');

if (document.getElementById('m').checked == true) {
  var Gender = 'Male';
} else {
  Gender = 'female';
}

function showData() {
  if (form.style.display != 'none') {
    form.style.display = 'none';
    edit.style.display = 'block';
  } else {
    form.style.display = 'block';
  }
  parname.innerHTML = [
    firstName.name + " : " + firstName.value + "<hr>",
    lastName.name + " : " + lastName.value + "<hr>",
    state.name + " : " + state.options[state.selectedIndex].text + "<hr>"
  ].join("");
}

function editData() {
  if (edit.style.display != 'none') {
    edit.style.display = 'none';
    form.style.display = 'block';
  } else {
    edit.style.display = 'block';
  }
}
<!DOCTYPE html>
<html>

<head>
  <title></title>
</head>

<body>
  <div id="form_div">
    <form>
      First Name:
      <input type="text" name="First Name" id="fname">Last Name:
      <input type="text" name="Last Name" id="lname">
      <input type="radio" name="Gender" id="m" value="male" checked>Male
      <input type="radio" name="Gender" id="f" value="female">Female
      <select name="state" id="select_state">
        <option value="select">- Please Select -</option>
        <option value="PA">Pennsylvania</option>
        <option value="NJ">New Jersey</option>
        <option value="WI">Wisconsin</option>
        <option value="WA">Washington</option>
      </select>
    </form>
    <input type="submit" value="Save" onclick="showData();">

  </div>

  <div id="view_div" style="display:none;">
    <button id="edit" value="Edit" onclick="editData();"></button>
    <p id="pname"></p>
  </div>

  <body>

</html>
Answer:1
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div id="form_div">
        <form>
            First Name:
            <input type="text" name="First Name" id="fname">
            Last Name:
            <input type="text" name="Last Name" id="lname">
            <input type="radio" name="Gender" id="m" value="male" checked>Male
            <input type="radio" name="Gender" id="f" value="female">Female
            <select name="state" id="select_state">
                <option value="select">- Please Select -</option>
                <option value="PA">Pennsylvania</option>
                <option value="NJ">New Jersey</option>
                <option value="WI">Wisconsin</option>
                <option value="WA">Washington</option>
            </select>
        </form>
       <input type="submit" value="Save" onclick="showData();">


    </div>

    <div id="view_div" style="display:none;">
        <button type="button" id="edit" value="Edit" onclick="editData();">
        <p id="pname"></p>
    </div>

    <script>
        var firstName = document.getElementById('fname');
        var lastName = document.getElementById('lname');
        var state = document.getElementById('select_state');
        var whichState = state.options[state.selectedIndex].text;
        var parname = document.getElementById('pname');
        var form = document.getElementById('form_div');
        var edit = document.getElementById('view_div');

        if (document.getElementById('m').checked == true) {
            var Gender = 'Male';
        } else {
            Gender = 'female';
        }

        function showData() {
            if (form.style.display != 'none') {
                form.style.display = 'none';
                edit.style.display = 'block';
            } else {
                form.style.display = 'block';
            }
            parname.innerHTML = [
                    firstName.name + " : " + firstName.value + "<hr>",
                    lastName.name  + " : " + lastName.value + "<hr>",
                    state.name     + " : " + state.options[state.selectedIndex].text + "<hr>"
                ].join("");
        }
        function editData() {
            if (edit.style.display != 'none') {
                edit.style.display = 'none';
                form.style.display = 'block';
            } else {
                edit.style.display = 'block';
            }           
        }
    </script>
</body>
</html>

try this one I think it is working

Answer:2

try the below code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div id="form_div">
        <form>
            First Name:
            <input type="text" name="First Name" id="fname">
            Last Name:
            <input type="text" name="Last Name" id="lname">
            <input type="radio" name="Gender" id="m" value="male" checked>Male
            <input type="radio" name="Gender" id="f" value="female">Female
            <select name="state" id="select_state">
                <option value="select">- Please Select -</option>
                <option value="PA">Pennsylvania</option>
                <option value="NJ">New Jersey</option>
                <option value="WI">Wisconsin</option>
                <option value="WA">Washington</option>
            </select>
        </form>
        <input type="submit" value="Save" onclick="showData();">

    </div>

    <div id="view_div" style="display:none;">
        <button type="button" id="edit" value="Edit" onclick="editData();">
        <p id="pname"></p>
    </div>

    <script>


        function showData() {
            var firstName = document.getElementById('fname').value;
        var lastName = document.getElementById('lname').value;
        var state = document.getElementById('select_state');
        var whichState = state.options[state.selectedIndex].text;
        var parname = document.getElementById('pname');
        var form = document.getElementById('form_div');
        var edit = document.getElementById('view_div');

        if (document.getElementById('m').checked == true) {
            var Gender = 'Male';
        } else {
            Gender = 'female';
        }
            if (form.style.display != 'none') {
                form.style.display = 'none';
                edit.style.display = 'block';
            } else {
                form.style.display = 'block';
            }
            parname.innerHTML = [
                    " First Name : " + firstName + "<hr>",
                    " Last Name : " + lastName + "<hr>",
                    " state : " + state.options[state.selectedIndex].text + "<hr>"
                ].join("");
        }
        function editData() {
            if (edit.style.display != 'none') {
                edit.style.display = 'none';
                form.style.display = 'block';
            } else {
                edit.style.display = 'block';
            }           
        }
    </script>
</body>
</html>
Answer:3
 var firstName = document.getElementById('fname');
var lastName = document.getElementById('lname');

Problem: you didn't target DOM objects, you have targeted their values, and later in code you try to get properties, like: firstName.name! Change two lines at the top of your code to this, and everything should work fine.

Answer:4

I tried to return data after update in firebase but it is displaying as undefined test().then((res) => { alert(res); }); test() { var fredNameRef = new Firebase('https://...

I tried to return data after update in firebase but it is displaying as undefined test().then((res) => { alert(res); }); test() { var fredNameRef = new Firebase('https://...

  1. return success data ajax jquery
  2. return success data
  3. return success data ajax
  4. javascript return success data
  5. ajax success return data json
  6. jquery success return data
  7. datatable ajax success return data
  8. ajax post success return data

In the past, I typically added my dialog as HTML as shown on the first example. I never liked having to maintain both the HTML page and the JavaScript page, and am thinking of using either the second ...

In the past, I typically added my dialog as HTML as shown on the first example. I never liked having to maintain both the HTML page and the JavaScript page, and am thinking of using either the second ...

I have a navbar in which I can't really see the .active color as I am using a 3rd party theme that I really like, but however I have one problem with the theme. I can't properly see which of my navbar ...

I have a navbar in which I can't really see the .active color as I am using a 3rd party theme that I really like, but however I have one problem with the theme. I can't properly see which of my navbar ...

  1. bootstrap change navbar color
  2. bootstrap change navbar text color
  3. bootstrap change navbar height
  4. bootstrap change navbar background color
  5. bootstrap change navbar toggler color
  6. bootstrap change navbar color on scroll
  7. bootstrap change navbar font family
  8. bootstrap change navbar width
  9. bootstrap change navbar brand color
  10. bootstrap change navbar dropdown color
  11. bootstrap change navbar collapse breakpoint
  12. bootstrap change navbar breakpoint
  13. bootstrap change navbar text size
  14. bootstrap change navbar item color
  15. bootstrap change navbar font color
  16. bootstrap change navbar active color
  17. bootstrap change navbar font
  18. bootstrap change navbar on scroll
  19. bootstrap change navbar collapse color

I'm using Kimono Labs to scrape data from a web page and create RSS output. Each item includes title, description and unique link. I want to use the feed as a trigger on IFTTT.com, but IFTTT requires ...

I'm using Kimono Labs to scrape data from a web page and create RSS output. Each item includes title, description and unique link. I want to use the feed as a trigger on IFTTT.com, but IFTTT requires ...