Antoine Bergeron Antoine Bergeron - 7 months ago 49
CSS Question

Can't make jQuery dynamic selector work

I'm working on what seems to be a simple script, making buttons toggle divs in and out of view. I've done so before, but this time I'm using dynamic selectors in jQuery, and can't see what is wrong.

If I make the call a static one (ie: '#liste_21'), it works like a charm, but as the number of item is not now in advance, I would prefer to get the dynamic version working.

Any idea what's wrong?


$(document).ready(function() {
$(".jour").click(function() {
var jour = $(this).text();


<div id="calendrierActivites">
<div id="nov20" class="jour">
<div id="nov21" class="jour">
<div id="nov22" class="jour">

<div id="liste_20">
<h2>20 novembre</h2>
<div id="liste_21">
<h2>21 novembre</h2>

<div id="liste_22">
<h2>22 novembre</h2>


JS Fiddle

var jour = $(this).find('span').text();

$(this).text() will get the text inside the div with class of jour and you're using the wrong selector.