Reputation: 1245
Good day, I want to attach click method to div, by BIND method. My code of attaching is
$("#brend_list > #mCSB_1 > .mCSB_container > div > a").bind('click',function(){
id=$(this).attr('id');
ttl=$(this).parent().children("span").html();
state=$(this).hasClass("checked");
if(state) {$(this).removeClass("checked");
$("#"+id+"_filter").remove();
}
else{$(this).addClass("checked");
$("#filters").append(" <div style='display:inline-block;' id='"+id+"_filter' class='filter_brands' onclick='brend_uncheck(\""+id+"\"); $(this).remove();'><span>"+ttl+"</span><b>x</b></div> ");
}});
My div structure is
<div id="brend_list" class="mCustomScrollbar _mCS_1">
<div class="mCustomScrollBox mCS-light" id="mCSB_1" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
<div class="mCSB_container" style="position: relative; top: 0px;">
<div><a id="filt_brend001"></a><span>Loreal Paris</span></div><!-- this <a> -->
<div><a id="filt_brend002"></a><span>Laura Mercier</span></div>
...........
</div>
<div class="mCSB_scrollTools" style="position: absolute; display: block;">
<div class="mCSB_draggerContainer"><div class="mCSB_dragger" style="position: absolute; height: 110px; top: 0px;" oncontextmenu="return false;">
<div class="mCSB_dragger_bar" style="position: relative; line-height: 110px;">
</div>
</div>
<div class="mCSB_draggerRail">
</div>
</div>
</div>
</div>
</div>
But it doesn't attach.
Upvotes: 1
Views: 353
Reputation: 80629
You should either change
$("#brend_list > #mCSB_1 > .mCSB_container > div > a")
to
$("#brend_list > #mCSB_1 > .mCSB_container > div > a + span")
A fiddle with above is here.
or, change the HTML from
<div><a id="filt_brend001"></a><span>Loreal Paris</span></div>
to
<div><a id="filt_brend001"><span>Loreal Paris</span></a></div>
Fiddle with above in action is here.
Upvotes: 2