javascript - Add disable to div class - Stack Overflow

I want to add and remove class disabled on button click.I tried to add it using but var r is not assig

I want to add and remove class disabled on button click. I tried to add it using but var r is not assigned correctly to the div.

var r = $("div.radio.ui-buttonset");
        alert(r)
        if(r) {
            r.className += r.className ? ' disabled' : 'disabled';
        }

HTML

<div class="input radio optional ui-buttonset">
   radio buttons
</div>

Desired HTML result on click:

<div class="input radio optional ui-buttonset disabled">
   radio buttons
</div>

Desired HTML result another click: (back to original)

<div class="input radio optional ui-buttonset">
   radio buttons
</div>

I want to add and remove class disabled on button click. I tried to add it using but var r is not assigned correctly to the div.

var r = $("div.radio.ui-buttonset");
        alert(r)
        if(r) {
            r.className += r.className ? ' disabled' : 'disabled';
        }

HTML

<div class="input radio optional ui-buttonset">
   radio buttons
</div>

Desired HTML result on click:

<div class="input radio optional ui-buttonset disabled">
   radio buttons
</div>

Desired HTML result another click: (back to original)

<div class="input radio optional ui-buttonset">
   radio buttons
</div>
Share Improve this question edited Aug 6, 2011 at 17:49 Immo asked Aug 6, 2011 at 17:44 ImmoImmo 6011 gold badge6 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If you're using jQuery, just use ".removeClass()":

r.removeClass("disabled");

To add the class back, there's ".addClass()":

r.addClass("disabled");

edit — Given the markup you posted, the selector you're using to get the target <div> element should work fine. It's looking for a <div> that's got the two classes "radio" and "ui-buttonset", and according to the HTML you posted that's correct. If it's not working, you need to describe how exactly it's not working.

$('div.radio.ui-buttonset').toggleClass('disabled');

Good luck!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745232888a4617758.html

相关推荐

  • javascript - Add disable to div class - Stack Overflow

    I want to add and remove class disabled on button click.I tried to add it using but var r is not assig

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信