views:

517

answers:

4

How would I do this without jQuery?

$('input[type=submit]').attr('disabled',true);

It doesn't have to be cross-browser compatible; a solution that only works in Firefox is OK.

+1  A: 
var els = document.getElementsByTagName ( 'input' );
for ( var i = 0; i < els.length ; i ++ ) {
 if ( els[i].type == 'submit' ) els[i].setAttribute('disabled', 'true'); 
}
Jamie
this gives error "els[i].setattribute is not a function"
Kip
It's setAttribute (captial 'A'). I corrected the code.
Patrick McElhaney
ok, downvote revoked. :)
Kip
+6  A: 
var inputs = document.getElementsByTagName("INPUT");
for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].type === 'submit') {
        inputs[i].disabled = true;
    }
}
RaYell
+1  A: 

This is untested, but it or something very similar should work. It could be made better with error and feature checking.

var inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++){
  if(inputs[i].type == 'submit'){
    inputs[i].disabled = 'disabled';
  }
}
Phairoh
needs to be "var i" in javascript, not "int i"
Kip
haha, whoops. Too many languages floating in my brain!
Phairoh
+1  A: 

Have you tried

document.getElementsByTagName("input");

then you could interrogate the DOM to find your submit button. getElementsByTagName reference

A full sample

window.onload = function(e) {
    var forms = document.getElementsByTagName('form');
    for (var i = 0; i < forms.length; i++) {
        var input = forms[i].getElementsByTagName('input');
        for (var y = 0; y < input.length; y++) {
            if (input[y].type == 'submit') {
                input[y].disabled = 'disabled';
            }
        }

    }
}
David Christiansen
Wow, in the time it took me to write that you got 4 answers ;) Guess it must have been an easy question ! :)
David Christiansen
thanks, but two things: you have a hard-coded "input[0]" that needs to be "input[i]", and .toLowerCase() apparently isn't necessary (at least for me in FF 3.5.1).
Kip
Fair comment Kip, Updated code.
David Christiansen
Why was this down voted, out of curiosity...
David Christiansen