admin管理员组

文章数量:1323323

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() ?

Share edited Mar 4, 2012 at 22:01 Soufiane Hassou 17.8k2 gold badges41 silver badges76 bronze badges asked Mar 4, 2012 at 21:56 wantTheBestwantTheBest 1,7204 gold badges46 silver badges71 bronze badges 5
  • 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
Add a ment  | 

2 Answers 2

Reset to default 7

Probably 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