event handling - jQuery .on() is giving me the wrong "this" -


probably slap-my-forehead solution, but:

<div id="foo">    <!-- replaced periodically via ajax -->    <p id="bar_1">click me</p>    <p id="bar_2">click me</p>    <p id="bar_3">click me</p>    <!-- end ajaxed section --> </div>  $('#foo').on(click,'p',function(){    alert($(this).attr('id'));    //returns "foo" }); 

clicking p alerts "foo". how return "bar_n", id of p clicked?

i'm targeting outer div because it's reliable , not going replaced via ajax. within .on() method, i'm targeting (subtargeting?) inner p because that's want bind click handler to. p's replaced periodically, , bindings lost, therefore, can't $('p').on(click...). can i?

you need add quote here click - 'click' , every thing fine then, fiddle submitted @arun.

try -

$('#foo').on('click','p',function(){    alert($(this).attr('id'));    //returns "foo" }); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -