Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  193] [ 3]  / answers: 1 / hits: 189261  / 13 Years ago, mon, january 16, 2012, 12:00:00

I'm trying to get the text from a text box.



I have 2 input text boxes that are not in a form, and I'm trying to retrieve the value and store it in a variable.



This code returns undefined in the alert box that pops up.



<script>
var userPass = document.getElementById('pass');
var userName = document.getElementById('fName');
function submit(){
alert(userPass.value);
}
</script>


When I run it with userName.value as a parameter in the alert function, it will work and display what ever you type in the box.



Here is the html:



 <table id=login>
<tr>
<td><label>User Name</label></td>
</tr>
<tr>
<td colspan=2><input class=textBox id=fName type=text maxlength=30 required/></td>
</tr>
<tr>
<td id=pass><label>Password</label></td>
<tr>
<td colspan=2><input class=textBox id=pass type=text maxlength=30 required/></td>
</tr>
<tr>
<td><input type=button class=loginButtons value=Login onclick=submit();/>&nbsp&nbsp&nbsp
<input type=button class=loginButtons value=Cancel/></td>
</table>

More From » html

 Answers
18

You will notice you have no value attr in the input tags.

Also, although not shown, make sure the Javascript is run after the html is in place.


[#88005] Friday, January 13, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Fri, Aug 13, 21, 00:00, 3 Years ago
Tue, Mar 30, 21, 00:00, 3 Years ago
Fri, Jun 5, 20, 00:00, 4 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
;