Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  43] [ 4]  / answers: 1 / hits: 15482  / 10 Years ago, wed, february 5, 2014, 12:00:00

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>

More From » jquery

 Answers
2

I 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


[#72705] Tuesday, February 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;