JoaoPedro
JoaoPedro

Reputation: 511

Element reference is always null on IE8

I´m trying to reference a submit button:

<input name="submit" type="submit" id="submit" tabindex="5" value="Submit" />

Tried this: var submit_btn = $('submit'); and also with document.getElementByID('submit'). But it always evaluates to null.. Also tried to loop all form elements to reach the button as lastChild but the result is allways null. Is this a known bug? Also, when I try to reference the form element by id, the result is also null..

Upvotes: 0

Views: 1368

Answers (5)

mtyson
mtyson

Reputation: 8550

Just ran into a similar IE8 problem. Most of the input elements were null inside a form when accessed via JS. Here's the markup:

<p>
  <form styleId="districtReportForm" method="post" onsubmit="return false;">
    <input name="reportType" value="0" type="hidden"> 
    <input name="districtId" value="30001176" type="hidden">

Worked fine in everything but IE8. In IE8, after 'reportType' there were no elements inside the form. For example, after getting the form by ID, form.districtId was null.

So I ran:

dojo.byId('districtReportForm').innerHTML

To see what IE thought was there, and it reported:

"<INPUT name=reportType value=0 type=hidden> </P>" 

Apparently IE had decided to close the opening <p> for me (admittedily it wasn't properly closed). Removing the paragraph element fixed this.

Upvotes: 0

Pointy
Pointy

Reputation: 413720

I bet IE just doesn't like the value "submit" for the "id" property of the element. Remember that IE wants to use the "id" value as a property name on the "window" object, and it might consider "submit" to be a reserved word. I know you say you can't change the value. If you're using jQuery, try$('input[name=submit]')

Upvotes: 2

Bryan A
Bryan A

Reputation: 3634

Try:

$("input#submit"); 

or:

$("input[type='submit']");

Also, it's considered poor practice to give ID's such as "Submit" -- be a bit more descriptive like: <input id="SubmitRegistration">

Remember to use the # selector to reference ID's -- you didn't in your statement above.

Also, remember to place your jQuery inside a $(document).ready(function() {}); to make sure it runs after the dom's loaded.

Upvotes: 0

St&#233;phan Kochen
St&#233;phan Kochen

Reputation: 19943

I assume the dollar function is the Prototype library? If not, in JQuery, the invocation should be $('#submit').

Or, are you trying this straight from within a <script> tag? You may have to wrap the code in an onLoad callback. If you do not, your browser may execute the script before the page has been fully loaded, and your element could still be missing from the DOM. Examples:

For Prototype:

Event.observe(window, 'load', function() {
    // your code
});

For JQuery:

$(document).ready(function() {
    // your code
});

Upvotes: 0

Pekka
Pekka

Reputation: 449415

  • Are you sure there is only one element with the ID submit on the page?
  • Can you give the element an ID different than "submit"? I don't think there should be any reserved names in CSS IDs, but just to make sure.

Upvotes: 0

Related Questions