Borsi
Borsi

Reputation: 301

getElementById returns null except in Internet Explorer

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!

Upvotes: 1

Views: 2054

Answers (5)

Magus
Magus

Reputation: 15104

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>

Upvotes: 2

Haris Bjelic
Haris Bjelic

Reputation: 117

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';

Upvotes: -4

Deepak Biswal
Deepak Biswal

Reputation: 4320

Because 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.

Upvotes: 8

Philipe Reichert
Philipe Reichert

Reputation: 1

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> 

Upvotes: 0

Saransh Kataria
Saransh Kataria

Reputation: 1497

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>

Upvotes: 2

Related Questions