How do I get the value of a textarea?
-
This is a follow up to http://stackoverflow.com/questions/8114926/can-i-test-xmlhttprequest-in-sdk-with-localhost. I am asking again because this seems more a javascript question than a Google App Engine question. I have a form <form name="choice_form" id="choice_form" method="post" action="/g/choicehandler" onsubmit="writeToStorage()"> <textarea name="choice" rows="7" cols="50"></textarea><br /> <input type="submit" value="submit your choice"> </form> I want to take the value of this textarea and send it to the app with formData. I tried this var choice = document.getElementById("choice_form").value; but I get "undefined" for the value of "choice". What am I doing wrong? And also, if I understand correctly, the /g/choicehandler is called twice once by the form and once by the XHR. How do I fix that? Below is the handler: class Choice(webapp.RequestHandler): def get(self): self.response.out.write(""" <html> <head> <script type="text/javascript"> var count = 0; function writeToStorage() { var user = "user" + count; count++; localStorage.setItem("chooser", user); var choice = document.getElementById("choice_form").value; var formData = new FormData(); formData.append("chooser", user); formData.append("choice", choice); var xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8086/g/choicehandler", true); xhr.onreadystatechange = function (aEvt) { if (xhr.readyState == 4 && xhr.status == 200){ console.log("request 200-OK"); } else { console.log("connection error"); } }; xhr.send(formData); //added this per Aaron Dufour's answer return 0; }; </script> </head> <body> //changed onsubmit like this: onsubmit="return writeToStorage(); as per Aaron Dufour's answer <form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()"> <textarea name="choice" rows="7" cols="50"></textarea><br /> <input type="submit" value="submit your choice"> </form> </body> </html>""") UPDATE See http://stackoverflow.com/questions/8116545/how-do-i-get-the-value-of-a-textarea/8116770#8116770 for the solution.
-
Answer:
As others have said, you're accessing the form, when the data you want is in the textarea. If you give the textarea an ID, that's probably the easiest way to get to its value. After much discussion, we determined that you don't need XHR at all. Here's what your form should look like: <form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()"> <textarea name="choice" rows="7" cols="50"></textarea><br /> <input type="hidden" name="chooser" id="form_chooser" /> <input type="submit" value="submit your choice"> </form> And now, we use the javascript function to edit the form before allowing it to be submitted: var count = 0; function writeToStorage() { var user = "user" + count; count++; localStorage.setItem("chooser", user); document.getElementById("form_chooser").value = user; };
Zeynel at Stack Overflow Visit the source
Other answers
choice_form is the <form>, not the <textarea>. You need to give the <textarea> an ID and use that ID instead.
SLaks
You can access the form formally using: document.forms['choice_form'] or also: document.forms.choice_form Each form has an elements collection that is all the form controls. You can access them much the same way: document.forms['choice_form'].elements['choice'] or document.choice_form.choice provided the names follow the rules for valid javascript identifiers. If they don't, you need to use square bracket notation: document['choice_form']['choice'] all return a reference to the element named 'choice' in a form with name 'choice_form'. So to get the value: document.choice_form.choice.value
RobG
(Some great answers here, but I haven’t seen one that puts it together with what you have…) Since a form can have more than one input, you need to access value on a particular one. There are a few ways to do this, but the most straightforward might be to use the form’s elements property (only forms have this!): document.getElementById("choice_form").elements.choice.value
Sidnicious
Just target the right element.
Shomz
Related Q & A:
- How can I get the value of dynamically created input?Best solution by Stack Overflow
- How do I get thinner legs and a flat stomach?Best solution by Yahoo! Answers
- How can i get backstage passes to a concert?Best solution by Yahoo! Answers
- How can I get in touch with a Mechanical Engineer?Best solution by nationalcareersservice.direct.gov.uk
- How can I get an interview with a Pilot?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.