Reputation: 497
I try to toggle a div inside a li but its not working for me
$('#open_p_table').on('click', function() {
$(this).closest('li').find('.players').toggle();
});
This is the FIDDLE
What i am doing wrong?
Upvotes: 0
Views: 602
Reputation: 86
use a class selector
$('.open_p_table').on('click', function() {
$(this).closest('li').find('.players').toggle();
});
ID's are normally suppose to be unique
Upvotes: 0
Reputation: 36784
Both span elements share this ID: open_p_table
.
IDs must be unique. Use classes instead:
$('.open_p_table').on('click', function () {
$(this).closest('li').find('.players').toggle();
});
<ul>
<li class="item"> <span class="open_p_table">Players</span>
<div class="players">description</div>
</li>
<li class="item"> <span class="open_p_table">Players</span>
<div class="players">description</div>
</li>
</ul>
Upvotes: 1
Reputation: 67197
Id should be uique..! change the open_p_table
as a class and do,
$('.open_p_table').on('click', function () {
$(this).closest('li').find('.players').toggle();
});
.open_p_table{ cursor:pointer; }
.open_p_table + .players { display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<ul>
<li class="item"><span class="open_p_table">Players</span>
<div class="players">description</div>
</li>
<li class="item"><span class="open_p_table">Players</span>
<div class="players">description</div>
</li>
</ul>
Upvotes: 1
Reputation: 494
You are using ID for selecter. However, if there are multiple same IDs in a DOM, this will select only the first element with that ID. You will need to use the class.
$('.open_p_table').on('click', function() {
$(this).closest('li').find('.players').toggle();
});
and your HTML will be
<ul>
<li class="item">
<span class="open_p_table">Players</span>
<div class="players">
description
</div>
</li>
<li class="item">
<span class="open_p_table">Players</span>
<div class="players">
description
</div>
</li>
</ul>
Upvotes: 0
Reputation: 4063
http://jsfiddle.net/oexf0624/1/
You were re-using an id
(which must be unique) - I changed it to a class
and now it works :)
$('.open_p_table').on('click', function() {
$(this).closest('li').find('.players').toggle();
});
<span class="open_p_table">Players</span>
<div class="players">
description
</div>
Upvotes: 1
Reputation: 4076
First Id's must be unique, so change it to class like:
$('.open_p_table').on('click', function() {
$(this).closest('li').find('.players').toggle();
});
And you HTML:
<li class="item">
<span class="open_p_table">Players</span>
<div class="players">
description
</div>
</li>
Upvotes: 1
Reputation: 18873
Instead of taking duplicate id open_p_table take class as shown :
HTML :
<ul>
<li class="item">
<span class="open_p_table">Players</span>
<div class="players">
description
</div>
</li>
<li class="item">
<span class="open_p_table">Players</span>
<div class="players">
description
</div>
</li>
</ul>
Jquery:
$('.open_p_table').on('click', function() {
$(this).closest('li').find('.players').toggle();
});
Upvotes: 1