admin管理员组文章数量:1023827
i have 3 radio buttons.what i need to do is that when one radio button is selected so i need to apply my css class name "line" on the text after it like
<span> <input type="radio" name="group1" value="Milk"> Milk </span>
i need to apply my class on Milk and when user select other radio button so the same class apply on other radio button text but remove class from the previous one.this is what i tried
<style type="text/css">
.line{
text-decoration: line-through;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input[type='radio']").change(function(){
if($(this).is(':checked'))
//i wana do this
$(this).addClass('line');
//if another is selected so do this
$(this).removeClass('line');
});
});
<div>
<span> <input type="radio" name="group1" value="Milk"> Milk </span> </br>
<span> <input type="radio" name="group1" value="Butter"> Butter </span> </br>
<span> <input type="radio" name="group1" value="Cheese"> Cheese </span> </br>
<hr>
</div>
i have 3 radio buttons.what i need to do is that when one radio button is selected so i need to apply my css class name "line" on the text after it like
<span> <input type="radio" name="group1" value="Milk"> Milk </span>
i need to apply my class on Milk and when user select other radio button so the same class apply on other radio button text but remove class from the previous one.this is what i tried
<style type="text/css">
.line{
text-decoration: line-through;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input[type='radio']").change(function(){
if($(this).is(':checked'))
//i wana do this
$(this).addClass('line');
//if another is selected so do this
$(this).removeClass('line');
});
});
<div>
<span> <input type="radio" name="group1" value="Milk"> Milk </span> </br>
<span> <input type="radio" name="group1" value="Butter"> Butter </span> </br>
<span> <input type="radio" name="group1" value="Cheese"> Cheese </span> </br>
<hr>
</div>
Share
Improve this question
asked Jun 14, 2012 at 8:34
Edward MayaEdward Maya
4392 gold badges10 silver badges25 bronze badges
2 Answers
Reset to default 4Since you need to add the class to the span
, you can use parent
to access the span
from the radio button. To remove the class from the other span
elements, just remove it from all which have the class in question before adding the class to the span
that was just selected:
$("input[type='radio']").change(function() {
if(this.checked) {
$('span.line').removeClass('line');
$(this).parent().addClass('line');
}
});
Here's a working example.
Note the use of this.checked
instead of the jQuery version you had. It is much faster to use the native DOM property where possible.
$("input[type='radio']").change(function() {
console.log(this.checked)
if (this.checked) {
// remove previously added line class
$('span.line').removeClass('line');
// you should add class to span, because text is
// within span tag, not in input
$(this).parent().addClass('line');
}
});
Working sample
i have 3 radio buttons.what i need to do is that when one radio button is selected so i need to apply my css class name "line" on the text after it like
<span> <input type="radio" name="group1" value="Milk"> Milk </span>
i need to apply my class on Milk and when user select other radio button so the same class apply on other radio button text but remove class from the previous one.this is what i tried
<style type="text/css">
.line{
text-decoration: line-through;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input[type='radio']").change(function(){
if($(this).is(':checked'))
//i wana do this
$(this).addClass('line');
//if another is selected so do this
$(this).removeClass('line');
});
});
<div>
<span> <input type="radio" name="group1" value="Milk"> Milk </span> </br>
<span> <input type="radio" name="group1" value="Butter"> Butter </span> </br>
<span> <input type="radio" name="group1" value="Cheese"> Cheese </span> </br>
<hr>
</div>
i have 3 radio buttons.what i need to do is that when one radio button is selected so i need to apply my css class name "line" on the text after it like
<span> <input type="radio" name="group1" value="Milk"> Milk </span>
i need to apply my class on Milk and when user select other radio button so the same class apply on other radio button text but remove class from the previous one.this is what i tried
<style type="text/css">
.line{
text-decoration: line-through;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("input[type='radio']").change(function(){
if($(this).is(':checked'))
//i wana do this
$(this).addClass('line');
//if another is selected so do this
$(this).removeClass('line');
});
});
<div>
<span> <input type="radio" name="group1" value="Milk"> Milk </span> </br>
<span> <input type="radio" name="group1" value="Butter"> Butter </span> </br>
<span> <input type="radio" name="group1" value="Cheese"> Cheese </span> </br>
<hr>
</div>
Share
Improve this question
asked Jun 14, 2012 at 8:34
Edward MayaEdward Maya
4392 gold badges10 silver badges25 bronze badges
2 Answers
Reset to default 4Since you need to add the class to the span
, you can use parent
to access the span
from the radio button. To remove the class from the other span
elements, just remove it from all which have the class in question before adding the class to the span
that was just selected:
$("input[type='radio']").change(function() {
if(this.checked) {
$('span.line').removeClass('line');
$(this).parent().addClass('line');
}
});
Here's a working example.
Note the use of this.checked
instead of the jQuery version you had. It is much faster to use the native DOM property where possible.
$("input[type='radio']").change(function() {
console.log(this.checked)
if (this.checked) {
// remove previously added line class
$('span.line').removeClass('line');
// you should add class to span, because text is
// within span tag, not in input
$(this).parent().addClass('line');
}
});
Working sample
本文标签:
版权声明:本文标题:javascript - jquery applying css class on select on radio buttons - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745538119a2155055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论