JavaScript HTML/Javascript Display User input (Refresh Issue) html javascript display none,html javascript display text,html javascript

I am trying to take what a user enters in a textbox, and then write that input to a paragraph on the page, and stop the page refreshing so that they can then copy that input for use elsewhere. (it's going to be a rather longer form, but this is a stumbling block i've hit
--edited for clarity--

All seems to be working well, other than when I enter input and press submit, the input is displaying fine for a couple seconds, then the page refreshes. Is there any way I can fix/stop this?

I have tried adding a while loop to the display for the code to stop when it has displayed, but so far no success.

Any pointers would be appreciated, thank you.

my code

<html>
<head>
<script type="text/javascript">
    function checkForm() {
        var input = document.myForm.username.value;
            document.write.getElementById("display").innerHTML=("Your username is" + " " + input);
            }


    </script>
</head>

<body>

<form name="myForm">
    <input type="text" name="username" value="Enter username" /><br/>
    <input type ="submit" value="Submit" 
    onClick="checkForm()" />

</form> 

<span id="display"><p> </p> </span>

</body>
</html>
Answer:1

Your function also needs to return false back to the html form:

function checkForm() {
    var input = document.myForm.username.value;
    document.write.getElementById("display").innerHTML=("Your username is" + " " + input);
    return false;
}

You should probably also be using the onsubmit event of the form itself, instead of onclick on the submit button.

Answer:2

mate, on a submit button, it defaults to submit your form that cause the page refresh, you need return false at the end of your checkForm(); or call e.preventdefault() at the end;

Answer:3

There is no document.write....

document.write.getElementById(...

It should be

document.getElementById(...

And you need to cancel the form submission.

document.forms[0].addEventListener("submit", function(evt) { evt.preventDefault(); });  

That needs to be attached on document ready or window onload.

Other option is to use

<form onsubmit="return false">
Answer:4

By using JavaScript or jQuery, I am trying to figure out if a word, say "casablanca", is available on the page? The page I am working on makes an ajax call which changes the content of the page. So I ...

By using JavaScript or jQuery, I am trying to figure out if a word, say "casablanca", is available on the page? The page I am working on makes an ajax call which changes the content of the page. So I ...

  1. searching word on web page

This my code when click the button i want to display button dynamic id to the specific place by calling that id: this is my code: <span id="2" class="view-details">View Details</span> $( ...

This my code when click the button i want to display button dynamic id to the specific place by calling that id: this is my code: <span id="2" class="view-details">View Details</span> $( ...

  1. jquery append not working with html

I have a simple jQuery slider but cant get multiple instances of it to work on the page, I am sure its an easy fix but my brain is just not co-operating atm function testSlide() { var testAnim = ...

I have a simple jQuery slider but cant get multiple instances of it to work on the page, I am sure its an easy fix but my brain is just not co-operating atm function testSlide() { var testAnim = ...

  1. simple jquery slider free download
  2. simple jquery slider codepen
  3. simple jquery slider plugin
  4. simple jquery slider example with source code
  5. simple jquery slider with next previous buttons
  6. simple jquery slider
  7. simple jquery slider w3schools
  8. simple jquery slider code
  9. simple jquery slider jsfiddle
  10. simple jquery slider tutorial
  11. simple jquery slider with dot navigation
  12. simple jquery slider responsive
  13. simple jquery slider example
  14. simple jquery slider autoplay
  15. simple jquery slider with thumbnails
  16. simple jquery slider with bullets
  17. simple jquery slider download
  18. simple jquery slider code example
  19. simple jquery slider with controls
  20. simple jquery slider left to right

Today I was wondering what would be the swiftest method to provide a cycle-through array in TypeScript, as in: ['one', 'two', 'three'] where the next value after three would be one, and I thought ...

Today I was wondering what would be the swiftest method to provide a cycle-through array in TypeScript, as in: ['one', 'two', 'three'] where the next value after three would be one, and I thought ...