Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey,
Am creating a Script for taking input from the Input Text Box and posting it in the webpage. What I am doing in Javascript is creating a new Element , adding the input value as the text node of the element , appending the text node to the element and then appending the element to another div. Here is the code :

HTML
<html>
<body>
<input type="text" id="typs" />
<input type="submit" value ="post " onclick="postf();">
<div id="container">
Posts
</div>
<script> 
function postf() {
var postEle = document.createElement("p");
var postCon = document.getElementById("typs").value;
var postTxt = document.createTextNode(postCon);
var con = document.getElementById("container");
postEle.appendChild(postTxt);
con.appendChild(postEle);
}
</script>
</body>
</html>


This code works perfectly taking the input and posting it to the webpage!
But when we put the <input> tags in a form tags :

HTML
<form name="input_form">
<input type="text" name="input_text" id="typs" />
<input type="submit" value ="post "  önclick="postf();">
</form>

This doesnt work properly.
When we click the Submit Button
The input is taken and the elemented is created, But it just flashes and goes! Just Flashes and goes! It just weird!
Why is this happening only when we insert the <form> tag?
Can anyone help me?

Thanking You,
digi0ps
Posted

1 solution

There are two types of Button. One is Submit Button and another is Normal Button.

You have used Submit Button as you have specified type="submit". And the behaviour of Submit Button is to GET/POST data (submit data) to server side if you have used inside the form tags.

As you are saying it is flashing, it is actually posts back and tries to send data of the page to the specified page in action attribute of form tag.

Refer - HTML form Tag[^].

In the example given in W3Schools, it is posting data to demo_form.asp page as it is mentioned inside the action attribute of form tag.

So, to avoid this, just use Normal Button, your issue will be resolved. Like below...
XML
<input type="button" value="post"  önclick="postf();"></input>
 
Share this answer
 
v3
Comments
digi0ps 27-May-13 13:56pm    
Thanks Tadit!
That helped! :)

I have this one one thing in my mind.
Cant the DOM Changes ( like creating or modifying elements) be there even if the web page reloads?
No. Think logically.

You are creating them dynamically on client side and when page loads it goes to server and fetches the new page again which don't have those elements. As you have created them on client side using javaScript, so how come the server will know them.

One way is to store that elements data in Session and check Session on PageLoad.
If Session exists, then fetch that and create element from code.

Is it understood ?
Please feel free to ask any doubts...
digi0ps 30-May-13 8:40am    
Okay ok! I havent yet gone into Session in Javascript ! :)

And one more thing, this isnt about JS.
How do you get this reputation points???
Sessions are server side concepts, although we can handle them on client side. But that is basically server side concept.

To check how reputation system works follow - http://www.codeproject.com/script/Membership/Reputation.aspx.
digi0ps 30-May-13 9:13am    
Hmmm...
And your points 25.6K --- thats WOW!
How did you earn that much points dude?
Are you in this forum for years?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900