Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:45:08+00:00 2026-05-26T02:45:08+00:00

i have a form which user enters some data, could be checkboxes, radio buttons,

  • 0

i have a form which user enters some data, could be checkboxes, radio buttons, textfields, etc

when user click submit button, i want to refresh the page with whatever data that was entered

<html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
    </head>
<body id="ref">
<form>
    Please enter your name:<input type="text" id="name" />
    Please enter your age:<input type="text" id="age" />
</form>
<input type="button" onclick="c()" value="submit">

<script type="text/javascript">

function c() 
{
    var o = document.getElementById('ref');
    o.innerHTML = '';

    var n = document.createElement('p');
    var nam = document.getElementById('name');
    n.innerHTML = "Your name is: " + nam;
    o.appendChild(n);

    var a = document.createElement('p');
    var ag = document.getElementById('age');
    a.innerHTML = "Your age is: " + ag;
    o.appendChild(a);

    //how do i get the info from the form? because both nam and ag are coming up null

}

</script>
</body>
</html>

my guess this is not working is because the page refreshes then tries to fetch the element by id which is not there anymore.. whats the correct way of doing this??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:45:08+00:00Added an answer on May 26, 2026 at 2:45 am

    You’re confusing objects with their properties. Here, you’re getting the HTMLInputElement instance for the “age” field:

    var ag = document.getElementById('age');
    

    But here you’re using that object as though it were a simple value:

    a.innerHTML = "Your age is: " + ag;
    

    The HTMLInputElement object has a value field you can use for that:

    a.innerHTML = "Your age is: " + ag.value;
    

    Separately, you’re completely destroying the page by doing this:

    var o = document.getElementById('ref');
    o.innerHTML = '';
    

    …because you’ve given the body element the ID “ref”. Completely replacing the body element completely replaces the body element, so you can’t rely on objects that only exist as subordinates of that element.

    The usual thing is to have an element to fill in, and (optionally) to remove the elements you no longer need. For instance (live copy):

    HTML:

    <form id="theForm">
        Please enter your name:<input type="text" id="name" />
        Please enter your age:<input type="text" id="age" />
        <input type="button" onclick="c()" value="submit">
    </form>
    <div id="result">
    </div>
    

    (Note I moved the button into the form for convenience.)

    JavaScript:

    function c() {
      var form = document.getElementById("theForm"),
          nameField = document.getElementById("name"),
          ageField = document.getElementById("age"),
          result = document.getElementById("result");
    
      form.parentNode.removeChild(form);
      result.innerHTML =
        "Your name is " + nameField.value +
        " and your age is " + ageField.value;
    }
    

    There, when the button is pressed, I remove the form and fill in the “result” div.

    You could add the “result” div dynamically if you wanted (live copy):

    HTML:

    <form id="theForm">
        Please enter your name:<input type="text" id="name" />
        Please enter your age:<input type="text" id="age" />
        <input type="button" onclick="c()" value="submit">
    </form>
    

    JavaScript:

    function c() {
      var form = document.getElementById("theForm"),
          nameField = document.getElementById("name"),
          ageField = document.getElementById("age"),
          result;
    
      result = document.createElement("div");
      result.innerHTML =
        "Your name is " + nameField.value +
        " and your age is " + ageField.value;
      form.parentNode.insertBefore(result, form);
      form.parentNode.removeChild(form);
    }
    

    You can access the fields using a briefer and somewhat more natural syntax if you change your id values to name values instead (live copy):

    HTML:

    <form name="theForm">
        Please enter your name:<input type="text" name="name" />
        Please enter your age:<input type="text" name="age" />
        <input type="button" onclick="c()" value="submit">
    </form>
    

    JavaScript:

    function c() {
      var form = document.theForm,
          nameField = form.name,
          ageField = form.age,
          result;
    
      result = document.createElement("div");
      result.innerHTML =
        "Your name is " + nameField.value +
        " and your age is " + ageField.value;
      form.parentNode.insertBefore(result, form);
      form.parentNode.removeChild(form);
    }
    

    Further reading:

    • DOM2 Core (well-supported by most modern browsers)
    • DOM2 HTML
    • DOM3 Core (increasingly supported)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form on my site which sends data to some remote site
I have a form in which the user can choose a component type from
I have a form in which the user selects a few items to display
I have got a form which a user can use to create a new
I have a form which takes both the user details and an image uploaded
I have a form which have many check boxes in it. User will check
I have an ASP.NET web form which I am adding a variable number User
I have a web form where I have a textbox in which the user
I have a form allowing a user to signup for a news letter which
I have a child page LoginContent.aspx which contains a login form. If the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.