Reputation: 51
I have a navigational menu now how can i add a selected class after click to any of my spans and remove selected class from previous span using Jquery.
<div class="large-12 columns TSCSlot">
<div class="large-10 columns DayRow">
<div class="large-12 columns left DaySelected">
<span class="Selected"></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
</div>
</div>
$('div.large-12.columns.left.DaySelected > span').click(function(e) {
e.preventDefault(); // prevent the default action
e.stopPropagation; // stop the click from bubbling
$(this).closest('span').find('.selected').removeClass('selected');
$(this).parent().addClass('selected');
});
span.Selected {
background-color: #5e9e37 !important;
color: white !Important;
}
Can anyone point me to what I'm doing wrong?
Upvotes: 0
Views: 102
Reputation: 93561
The target of your click is the span, so closest('span')
will find itself, so your following find('.selected')
will not find the "selected" element:
Try this instead: http://jsfiddle.net/TrueBlueAussie/6pg5as4n/
$(this).closest('.DaySelected').find('.selected').removeClass('selected');
$(this).addClass('selected');
Html:
<div class="large-12 columns TSCSlot">
<div class="large-10 columns DayRow">
<div class="large-12 columns left DaySelected">
<span class="selected"></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
</div>
</div>
Notes:
closest
with .DaySelected
will ensure you do not modify other controls on the page. Your could also just use parent()
e.g. $(this).parent().find('.selected').removeClass('selected')
selected
class to the clicked span
.S
on selected
in the html. Have changed that to lowercase s
to match the code.e.preventDefault()
and e.stopPropagation
you can simply return false
to do both. e.g. http://jsfiddle.net/TrueBlueAussie/6pg5as4n/2/Upvotes: 2
Reputation: 951
Some small improvements:
<span class="Selected"</span>
should be <span class="selected"></span>
(small beginning character for classes and > added)
$('div.large-12.columns.left.DaySelected > span').click(function(e) {
e.preventDefault(); // prevent the default action
e.stopPropagation; // stop the click from bubbling
$(this).parent().find('.selected').removeClass('selected'); //removes all selected classes in this div
$(this).addClass('selected');
});
Your click
is binded on the <span>
. So if you use jQuery(this)
it is actually the span you want to change. So you dont need .parent()
.find()
or anything else to add the class to it.
Upvotes: 1
Reputation: 9637
As You telling in post. You are clicking the current span You can add selected class ,remaining selected class are removed
$('div.large-12.columns.left.DaySelected > span').click(function(e) {
e.preventDefault(); // prevent the default action
e.stopPropagation; // stop the click from bubbling
$('.Selected').removeClass('Selected');
$(this).addClass('Selected');
});
NOTE :in css Selected not selected class
Upvotes: 1
Reputation: 125
I think your DOM select is wrong.
$('div.large-12.columns.left.DaySelected > span')
should be
$('div.large-12.columns .left.DaySelected > span')
Upvotes: -2