admin管理员组文章数量:1323505
A call to getElementById()
to retrieve a 'hidden' field on a form is returning null
.
I've studied other SO questions and many were using the hidden field's 'name' instead of 'id' in the call to getElementById()
or were executing getElementById()
in a script at top-of-file before the actual html containing the hidden field had been processed by the browser. Both of those errors would explain why getElementById()
was returning null
for them.
I could be wrong but I don't think that's my case here.
This is my form:
<form name="theDeleteItemForm" id="deleteItemForm" action="deleteTheSelectedItem"
method="post"
<input type="hidden" id="theHiddenField" name="deleteThisSelectedItem" value="">
</form>
And here is a Javscript handler that is successfully executing when a 'delete' button on the page is clicked (I can tell because my alert()
boxes in the handler function below are popping up):
function deleteItem()
{
alert("Just entered deleteItem()");
var theFieldToDelete = document.getElementById('theHiddenField');
// THIS IS THE PLACE WHERE I FOUND THAT 'theFieldToDelete' WAS 'null'
alert("Just got the hidden field element, which is: " + theFieldToDelete );
// THIS DOES NOTHING MORE THAN TO PREVENT THE 'alert' THAT FOLLOWS FROM APPEARING
theFieldToDelete.value = "upForDeletion";
alert("deleteItem() was called, about to submit the form");
document.theDeleteItemForm.submit();
}
I'm not understanding why I get the null
return from getElementById()
. I'm under the impression that
getElementById()
works FINE for type="hidden"
fields.
Furthermore, the id I used for my hidden field -- theHiddenField
-- is 100% unique in my file.
Why am I getting null
when I try to get the hidden field via getElementById()
?
A call to getElementById()
to retrieve a 'hidden' field on a form is returning null
.
I've studied other SO questions and many were using the hidden field's 'name' instead of 'id' in the call to getElementById()
or were executing getElementById()
in a script at top-of-file before the actual html containing the hidden field had been processed by the browser. Both of those errors would explain why getElementById()
was returning null
for them.
I could be wrong but I don't think that's my case here.
This is my form:
<form name="theDeleteItemForm" id="deleteItemForm" action="deleteTheSelectedItem"
method="post"
<input type="hidden" id="theHiddenField" name="deleteThisSelectedItem" value="">
</form>
And here is a Javscript handler that is successfully executing when a 'delete' button on the page is clicked (I can tell because my alert()
boxes in the handler function below are popping up):
function deleteItem()
{
alert("Just entered deleteItem()");
var theFieldToDelete = document.getElementById('theHiddenField');
// THIS IS THE PLACE WHERE I FOUND THAT 'theFieldToDelete' WAS 'null'
alert("Just got the hidden field element, which is: " + theFieldToDelete );
// THIS DOES NOTHING MORE THAN TO PREVENT THE 'alert' THAT FOLLOWS FROM APPEARING
theFieldToDelete.value = "upForDeletion";
alert("deleteItem() was called, about to submit the form");
document.theDeleteItemForm.submit();
}
I'm not understanding why I get the null
return from getElementById()
. I'm under the impression that
getElementById()
works FINE for type="hidden"
fields.
Furthermore, the id I used for my hidden field -- theHiddenField
-- is 100% unique in my file.
Why am I getting null
when I try to get the hidden field via getElementById()
?
-
4
Doubt it's the cause, but your
<input>
tag isn't being closed. The opening<form>
tag is also missing its>
. – Sir Crispalot Commented Mar 4, 2012 at 22:01 - 1 Fix your HTML and this should work properly. – Michael Berkowski Commented Mar 4, 2012 at 22:03
- 1 And make sure your IDs are unique – mplungjan Commented Mar 4, 2012 at 22:10
- What happens if you unhide the field? Does the problem still happen? – jamesmortensen Commented Mar 4, 2012 at 22:11
- Holy guacamole. Burned 15 minutes of life on a missing '>' to close the opening <form> tag. The <input> doesn't need to be closed as per w3schools./tags/tag_input.asp at least for HTML. Thanks guys -- I got into a staring contest with my code and lost, closing the <form by changing the above code to method="post"> solved the problem -- I'm now getting a non-null return element from getElementId -- thank you. – wantTheBest Commented Mar 4, 2012 at 22:19
2 Answers
Reset to default 7Probably it's because your <form>
element isn't formed properly (as in quoted example - the >
is missing in the opening tag)?
use document.getElementById('theHiddenField').value
本文标签: javascriptinput typequothiddenquot cannot be obtained from getElementByIdStack Overflow
版权声明:本文标题:javascript - input type="hidden" cannot be obtained from getElementById - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141791a2422610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论