I have a problem with the javascript document.getElementById function. The problem is, that every browser, except Internet Explorer, is getting an error that document.getElementById is null.
For Example Firefox:
TypeError: document.getElementById(...) is null
The getElementById-Function appears after the declaration of the button, so it shouldn't be a problem, that the function doesn't know what the ID-element is.
This is an extract of the script with the regarding code:
<html>
<head>
<title>Timeline</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" id="myform" onsubmit="window.location.reload();">
<input type="hidden" id="client_timestamp" name="client_timestamp" />
<button name= "subm_myform" type="submit" >Send My Time</button>
</form>
<script type="text/javascript">
// ------- where the error occurs ----------------
document.getElementById('subm_myform').style.visibility='hidden';
var mySync = setTimeout( function () {document.getElementById('subm_myform').click()} ,60000);
</script>
</body>
</html>
Thank you!
I have a problem with the javascript document.getElementById function. The problem is, that every browser, except Internet Explorer, is getting an error that document.getElementById is null.
For Example Firefox:
TypeError: document.getElementById(...) is null
The getElementById-Function appears after the declaration of the button, so it shouldn't be a problem, that the function doesn't know what the ID-element is.
This is an extract of the script with the regarding code:
<html>
<head>
<title>Timeline</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" id="myform" onsubmit="window.location.reload();">
<input type="hidden" id="client_timestamp" name="client_timestamp" />
<button name= "subm_myform" type="submit" >Send My Time</button>
</form>
<script type="text/javascript">
// ------- where the error occurs ----------------
document.getElementById('subm_myform').style.visibility='hidden';
var mySync = setTimeout( function () {document.getElementById('subm_myform').click()} ,60000);
</script>
</body>
</html>
Thank you!
Share Improve this question asked Nov 2, 2015 at 10:54 BorsiBorsi 3011 gold badge2 silver badges11 bronze badges 2-
1
< button name= "subm_myform">
/getElementById('subm_myform')
- maybe IE conflatesname
andid
for some backwards patibility reasons? I there's no element withid="subm_myform"
in the code you've posted. – pawel Commented Nov 2, 2015 at 10:55 -
1
Your button has a
name
ofsubm_myform
, not an ID. If IE doesn't find an element by ID, it tries to do so by name. At least in older versions of IE. (we actually had to shim the getElementByID function to so this in every browser for some of our code base once... shudder) – James Thorpe Commented Nov 2, 2015 at 10:56
5 Answers
Reset to default 8Because you are fetching DOM element by id
and looks like you DOM element has not any id
attribute. It should be <button name= "subm_myform" type="submit" id="subm_myform" >Send My Time</button>
.
This is a 'feature' of IE. Their implementation of getElementById
initially searches for elements with the given id
attribute. If none are found, it then searches for elements by the name
attribute.
If you want to find elements by their name, use the getElementsByName()
method instead.
There is no form element with id subm_myform, you have one with name. Fix that in the code as:
<button id= "subm_myform" type="submit" >Send My Time</button>
document.getElementById
is not suppose to look for the name
. It's weird that Internet Explorer does not crash. Maybe it try to find with the name if it can't find with the id
.
Add an id
to your button to fix the problem :
<button name="subm_myform" id="subm_myform" type="submit" >Send My Time</button>
Your
<button name= "subm_myform" type="submit" >Send My Time</button>
has no id, try this
<button id="subm_myform" name="subm_myform" type="submit" >Send My Time</button>
The problem is you want to get an id but you didn't add the # And the other problem is you give it a name attribute, you need to set it to id="subm_myform"
Try
<button id= "subm_myform" type="submit" >
document.getElementById('#subm_myform').style.visibility='hidden';