views:

255

answers:

4

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

A: 
  • 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.
Pekka
Only one. And i cant change the markup.. I need some other way to reference the element.
JoaoPedro
A: 

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.

Plan B
tried that now: $("input#submit") but still null. I cant change the id on the markup to keep it degradable. Thanks.
JoaoPedro
@JoaoPedro: Changed my answer a bit, check it out.
Plan B
A: 

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
});
Shtééf
Yep. Its prototype. and its running in an onload callback just like you said. thks.
JoaoPedro
+2  A: 

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]')

Pointy
And in Prototype, use the double dollar function: `$$('input[name=submit]')`
Shtééf