admin管理员组文章数量:1415484
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
2 Answers
Reset to default 2If 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!
本文标签: javascriptAdd disable to div classStack Overflow
版权声明:本文标题:javascript - Add disable to div class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232887a2648904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论