Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  28] [ 1]  / answers: 1 / hits: 41523  / 14 Years ago, tue, may 11, 2010, 12:00:00

I've been using



document.forms[0].fieldname.value


to get values in javascript from a form, but I'd like to use a name to reference the field and not a 0, what would be the equivalent these days since <form name=formname> isn't compliant?


More From » forms

 Answers
4

The easiest way is to add an id to the input: <input id=fieldname /> and reference the value from JavaScript like so:



document.getElementById('fieldname').value


or, if you're using jQuery



$('#fieldname').val();

[#96814] Saturday, May 8, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;