Reputation: 47
I would like to colored all divs except that one I click on. In my code it works, but only one time. If I click to another div, it doesnt work.
http://jsfiddle.net/6VhK8/353/
<div class="fp">
<div id="1">id="1"</div>
<div id="2">id="2"</div>
<div id="3">id="3"</div>
<div id="4">id="4"</div>
<div id="5">id="5"</div>
<div id="6">id="6"</div>
<div id="7">id="7"</div>
</div>
$(function() {
$('div').click(function() {
var r = this.id;
$('.fp div').not('#'+r).css({'color':'blue'});
});
});
Upvotes: 2
Views: 167
Reputation: 1717
Once a tag is blue, how will it change back to black ?
You are supposed to change color back to black before or after clicking for second time.
Upvotes: -1
Reputation:
Try using css class instead:
$(function() {
var divs = $('div.fp').find('div');
divs.on('click', function() {
divs.removeClass('blue');//remove previous selection
$(this).addClass('blue');//the current clicked div
});
});
.blue {
background: skyblue;
}
div.fp div {
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="fp">
<div id="1">id="1"</div>
<div id="2">id="2"</div>
<div id="3">id="3"</div>
<div id="4">id="4"</div>
<div id="5">id="5"</div>
<div id="6">id="6"</div>
<div id="7">id="7"</div>
</div>
Upvotes: 1
Reputation: 4288
The easiest way. Check your updated fiddle:
$(function() {
$('.fp div').on('click', function() {
$(this).css('color', 'black');
$('.fp div').not(this).css({'color':'blue'});
});
});
Upvotes: 0
Reputation: 864
check this fiddle hope this is what you are looking for;
$(function() {
$('.fp div').click(function() {
$('.fp div').css({'color' : 'blue'});
$(this).css({'color' : 'black'});
});
});
<div class="fp">
<div id="1">id="1"</div>
<div id="2">id="2"</div>
<div id="3">id="3"</div>
<div id="4">id="4"</div>
<div id="5">id="5"</div>
<div id="6">id="6"</div>
<div id="7">id="7"</div>
</div>
Upvotes: 3
Reputation: 388316
You can use a class
.highlight{
color:blue;
}
then
$(function () {
$('.fp div').click(function () {
$(this).removeClass('highlight').siblings().addClass('highlight')
});
});
Upvotes: 0