tags:

views:

73

answers:

3

Hi, I have n buttons. This button has the same name: setAlg.

I would disable all the buttons that has this name.

I have tried

$("input[name='setAlg']").attr("disabled",true);

but it doesn't work.

How I can do?

Thanks a lot.

A: 

I think you forgot quotes

$("input[name='setAlg']").attr('disabled','true');

If this doesn't work then maybe your selection doesn't work

Shervin
`true` *shouldn't* have quotes :)
Nick Craver
@Nick Craver: We use it like this, and it works just fine
Shervin
If he's using an XHTML Doctype using the string 'disabled' is recommended.
Denis 'Alpheus' Čahuk
@Shervin - That's because JavaScript has weak typing and any non-empty string is equivalent to `true`, "kdhfaisfbaudbvuiasdnvs" also works, that doesn't mean it's the most correct way :)
Nick Craver
@Denis - Attributes in markup != DOM boolean properties...this is a boolean property.
Nick Craver
I think that instead of 'true' you can use any non-empty string.
hluk
@Nick `setAttribute` in javascript is a (string, string) function, furthermore jQuery (as of 1.4.2) explicitly casts all values to strings when setting them with `attr`. Just because the DOM2 spec lists it as a boolean doesn't imply that it actually 'works' in most browser. Correct me if I'm wrong. As for using 'disabled' as the value, I agree - it is mostly for the HTML presentation, not the DOM itself.
Denis 'Alpheus' Čahuk
@Denis - I think you're confusing the DOM with markup, they're two very different things. It's a boolean property...so any non-empty string works, regardless of how it's set (weak typing dictated the method signature). For example do `.attr("disabled")` you'll get `true`/`false` back, not a string...just like casting going in, there's *also* a reason jQuery explicitly *returns* a bool in these cases :)
Nick Craver
+1  A: 

Chris Pebbles suggestion should work fine, but I am confused. In your question's title, you specificall state "jQuery disable all button of a css class", however, you then provide an example which shows that you really want to disable all buttons that have a name attribute of setAlg.

Two points come to mind. First, your buttons, since they all have the same name may produce unexpected results for you when the user submits your form (if this is what they do, but I may be wrong here). If this is the case, the you probably really do want to set all of them to the same class, not name.

<input class="setAlg">...

Second point is that if you do want to make your jQuery to work with a class, you'll need to do:

$("input.setAlg").attr("disabled","disabled");

I hope this was helpful.

mkoistinen
Thanks a lot...
michele
A: 

Make sure to wrap your code in ready handler:

$(function(){
  $("input[name='setAlg']").attr("disabled", true);
});

But If you are using the same class for those input fields (as per your question title), you need to try this instead:

$(function(){
  $("input.myclass").attr("disabled", true);
});

Where myclass is supposed to be the class name used for those input fields.

Sarfraz
Thanks a lot!...
michele
@michele: Welcome :)
Sarfraz
Classic gotcha :)
Denis 'Alpheus' Čahuk