最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add disable to div class - Stack Overflow

programmeradmin1浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论