admin管理员组文章数量:1268534
I need to add a class to the input wrapper when I click on input, and remove the class when other input is clicked.
I've tiyed toggleClass but it would only remove the class when the same input is clicked again.
I've tried to use functions: find() and closest() but cannot figure exactly which one I need, or in what order they shoudl be called... Any help would be appreciated.
So far here is the code:
<div class="wrapper">
<label for="name" class="required">
name
</label>
<div class="data_area">
<input type="text" id="" name="" value="" title="" class="" />
</div>
</div>
<div class="wrapper">
<label for="other name" class="required">
last name
</label>
<div class="data_area">
<input type="text" id="" name="" value="" title="" class="" />
</div>
</div>
<script>
$('.wrapper input').click(function () {
$(this).parent().addClass('red');
});
</script>
<style>
.red {background:red;}
</style>
I need to add a class to the input wrapper when I click on input, and remove the class when other input is clicked.
I've tiyed toggleClass but it would only remove the class when the same input is clicked again.
I've tried to use functions: find() and closest() but cannot figure exactly which one I need, or in what order they shoudl be called... Any help would be appreciated.
So far here is the code:
<div class="wrapper">
<label for="name" class="required">
name
</label>
<div class="data_area">
<input type="text" id="" name="" value="" title="" class="" />
</div>
</div>
<div class="wrapper">
<label for="other name" class="required">
last name
</label>
<div class="data_area">
<input type="text" id="" name="" value="" title="" class="" />
</div>
</div>
<script>
$('.wrapper input').click(function () {
$(this).parent().addClass('red');
});
</script>
<style>
.red {background:red;}
</style>
Share
Improve this question
asked Feb 5, 2014 at 16:18
mlclmmlclm
7256 gold badges17 silver badges39 bronze badges
1 Answer
Reset to default 10I think what you are looking for is focus/blur rather than click
$('.wrapper input').focus(function () {
$(this).parent().addClass('red');
}).blur(function () {
$(this).parent().removeClass('red');
});
Demo: Fiddle
If you still want click then try this
本文标签: javascriptjquery add class on input click and remove class when click other inputStack Overflow
版权声明:本文标题:javascript - jquery add class on input click and remove class when click other input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741094783a2338612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论