-1

I have a question to you about JavaScript. I want to display form after clicking a button and then when I write something and click submit it must display the value of form on screen. And the problem is that when I click submit it display only for a second. Here is my JavaScript code:

window.onload = function () {
    var dod = document.getElementById("dodaj")
    dod.onclick = function () {
        document.write("<form action='?' id='formualrz'><input id='f1' type='text' name='pole' value='Nazwa'><input id='f2' type='text' name='pole' value='Opis'><input type='submit' id='sub'></form>");
        var f1 = document.getElementById("f1");
        var f2 = document.getElementById("f2");
        var submit = document.getElementById("sub");

        submit.onclick = function () {
            document.write(f1.value + "<br/>" + f2.value);
        }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • It's submitting the form when you click the submit button, which is why you only see the results for a short amount of time. Plenty of information already available on how to prevent form submission when clicking the submit button. – Anthony Grist Mar 07 '14 at 10:09
  • It was created by the javascript men upon loading. – Stuart Mar 07 '14 at 10:13
  • Always be sure to read the descriptions that appear when selecting tags! – Charles Mar 07 '14 at 17:22

1 Answers1

0

Your form is being submitted. Add the following to your form tag

<form onsubmit="return(false)" >

Or in your case on the event handler:

submit.onclick = function () {
     document.write(f1.value + "<br/>" + f2.value);
     return(false);
}

This topic explains what return false actually does.

Community
  • 1
  • 1
Anonymoose
  • 2,389
  • 6
  • 36
  • 69