Jump to content

Grab the Text From a Text Field


ScottyBoy

Recommended Posts

How can I get the value of a text field from a form on my site? I'm trying to save that value in a JS variable i.e, $text = "textFromTheField";. Thanks, guys. :)

Link to comment
Share on other sites

document.getElementById("id_of_field").value

document.getElementById("id_of_form").name_of_field.value //older way, if you don't want to ID every field

Link to comment
Share on other sites

If they couldn't, jQuery would look a lot different.OTOH, if you mix this stuff with PHP, things could easily get confusing. I personally reserve the $ for private namespaces (i.e., a global object the rounds up a whole bunch of values instead of having a whole lot of independent global variables). This is a common practice.

Link to comment
Share on other sites

If they couldn't, jQuery would look a lot different.OTOH, if you mix this stuff with PHP, things could easily get confusing. I personally reserve the $ for private namespaces (i.e., a global object the rounds up a whole bunch of values instead of having a whole lot of independent global variables). This is a common practice.
I would love to do this with PHP, but unfortunately, the site I'm working on in is one of those free CMS websites that doesn't allow PHP.
Link to comment
Share on other sites

Kinda'.I'm posting my code here so you can help me out with it. I'm not sure what I'm doing wrong. :)

<html><head><script type="text/javascript">var text;function post() {text == document.getElementById('textField').value;}</script></head><body> <div align="center"> <div style="width:500px;height:250px;border:2px solid #000;"> <script type="text/javascript"> document.write(text); </script> </div> <br /><br /> <form onSubmit="post()" name="messageBoard"> <input id="textField" type="text" value="Type Your Message" /> <input type="submit" value="Submit" /> </form> </div>  </body> </html>

Link to comment
Share on other sites

When your page loads, the global variable text is declared but not initialized. The function post is declared but not executed. The document.write method is executed. It is passed the variable text, which at this point has no value, so either nothing is written, or perhaps the word "undefined" is written.When the form is submitted, the function post is executed. The function assigns a value to the variable text and does nothing else. Because post returns no value, the form will attempt to be submitted. Since there is no action attribute in the form tag, I'm not sure what happens after that. You probably can't tell the difference anyway, unless something flickers.What would you like to happen?

Link to comment
Share on other sites

All I'm trying to do is make so that when I type something into the text field and hit submit, the text I entered is saved as the value of the variable "text" (this is why I didn't add a preset value to it). And I want that variable to keep it's value, even if the page is reloaded. Until someone else comes along and changes it again. Does that make sense?

Link to comment
Share on other sites

JavaScript variables are stateless and contained within the script - they won't "survive" the reloading. You'll need to use a server-side storage method, or cookies, to do what you want.

Link to comment
Share on other sites

Yes, but you'd need to write to the XML file using a server-side language. JS can't do it.

Link to comment
Share on other sites

Have you investigated cookies?
A little. It seems complicated, and I'm not sure how to use them correctly though. I already looked at the W3Schools tutorial for using cookies with JavaScript, but I need something a little more explanatory.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...