admin管理员组文章数量:1026989
I looked and didn't find the case I have here -- a lot of the prior answers said 'wait for the 'onload' to call getElementById' but that's what I started with and it doesn't work -- getElementById returns null in this code in my index.php file:
<html>
<script type="text/javascript">
function checkPwd()
{
var thePwd = document.getElementById("theUsersPassword");
alert("the var thePwd is: " + thePwd);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beta:</title>
</head>
<body onload="checkPwd()">
<form method="post" action="index.php">
Beta: <input name="theUsersPassword" type="password"><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
I looked and didn't find the case I have here -- a lot of the prior answers said 'wait for the 'onload' to call getElementById' but that's what I started with and it doesn't work -- getElementById returns null in this code in my index.php file:
<html>
<script type="text/javascript">
function checkPwd()
{
var thePwd = document.getElementById("theUsersPassword");
alert("the var thePwd is: " + thePwd);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beta:</title>
</head>
<body onload="checkPwd()">
<form method="post" action="index.php">
Beta: <input name="theUsersPassword" type="password"><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Share
Improve this question
asked Nov 22, 2011 at 19:07
wantTheBestwantTheBest
1,7204 gold badges46 silver badges71 bronze badges
2
-
3
Alert statement should be
alert("the var thePwd is: " + thePwd.value);
– fardjad Commented Nov 22, 2011 at 19:10 - okay now thePwd is HTMLInputElement but alert("The password's value entered was " + thePwd.value) and alert("The password's innerHTML entered was " + thePwd.innerHTML) are both empty after I type 'foo' in the password field -- is my form's post wiping out the contents somehow? – wantTheBest Commented Nov 22, 2011 at 19:29
3 Answers
Reset to default 6getElementById get an element by ID not by name. You must change
<input name="theUsersPassword" type="password">
to
<input name="theUsersPassword" id="theUsersPassword" type="password" />
You are mixing up name
and id
Edit:
In other words, you need to change
<input name="theUsersPassword" type="password">
to
<input name="theUsersPassword" id="theUsersPassword" type="password">
You can use the code below. getElementById
returned an object not a value.
JavaScript
function checkPwd()
{
var thePwd = document.getElementById("theUsersPassword").value;
alert("the var thePwd is: " + thePwd);
}
Html
<input name="theUsersPassword" id="theUsersPassword" type="password">
I looked and didn't find the case I have here -- a lot of the prior answers said 'wait for the 'onload' to call getElementById' but that's what I started with and it doesn't work -- getElementById returns null in this code in my index.php file:
<html>
<script type="text/javascript">
function checkPwd()
{
var thePwd = document.getElementById("theUsersPassword");
alert("the var thePwd is: " + thePwd);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beta:</title>
</head>
<body onload="checkPwd()">
<form method="post" action="index.php">
Beta: <input name="theUsersPassword" type="password"><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
I looked and didn't find the case I have here -- a lot of the prior answers said 'wait for the 'onload' to call getElementById' but that's what I started with and it doesn't work -- getElementById returns null in this code in my index.php file:
<html>
<script type="text/javascript">
function checkPwd()
{
var thePwd = document.getElementById("theUsersPassword");
alert("the var thePwd is: " + thePwd);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beta:</title>
</head>
<body onload="checkPwd()">
<form method="post" action="index.php">
Beta: <input name="theUsersPassword" type="password"><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Share
Improve this question
asked Nov 22, 2011 at 19:07
wantTheBestwantTheBest
1,7204 gold badges46 silver badges71 bronze badges
2
-
3
Alert statement should be
alert("the var thePwd is: " + thePwd.value);
– fardjad Commented Nov 22, 2011 at 19:10 - okay now thePwd is HTMLInputElement but alert("The password's value entered was " + thePwd.value) and alert("The password's innerHTML entered was " + thePwd.innerHTML) are both empty after I type 'foo' in the password field -- is my form's post wiping out the contents somehow? – wantTheBest Commented Nov 22, 2011 at 19:29
3 Answers
Reset to default 6getElementById get an element by ID not by name. You must change
<input name="theUsersPassword" type="password">
to
<input name="theUsersPassword" id="theUsersPassword" type="password" />
You are mixing up name
and id
Edit:
In other words, you need to change
<input name="theUsersPassword" type="password">
to
<input name="theUsersPassword" id="theUsersPassword" type="password">
You can use the code below. getElementById
returned an object not a value.
JavaScript
function checkPwd()
{
var thePwd = document.getElementById("theUsersPassword").value;
alert("the var thePwd is: " + thePwd);
}
Html
<input name="theUsersPassword" id="theUsersPassword" type="password">
本文标签: javascriptonload call to getElementById returns nullStack Overflow
版权声明:本文标题:javascript - onload call to getElementById returns null - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659771a2161817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论