on click not working on dynamically created element


As i know and understand that for dynamically created element, we need to write jquery function like

//.class is a element which is added dynamically in page using ajax call
$(".Class").on('click',function(){
//Code to execute here
alert("in here"); //not able to call it


})

but when i am trying to do it, i am not able to call this function.
Am i doing this correctly or something is wrong?

 


Asked by:- jaya
2
: 608 At:- 6/4/2017 11:16:10 AM
jquery dynamic created element






2 Answers
profileImage Answered by:- jaiprakash

ok, i have faced this issue, and i can solve your problem quickly

you need to write function like this

//.class is a element which is added dynamically in page using ajax call?
$(document).on('click',".Class",function(){?
//Code to execute here?
alert("in here");· //not able to call it?

});


//that's it you are done

Important points

  $("element") element must be loaded with page, means instead of document in above code you can replace it with any element id, which is loaded with page, and should be parent of dynamically added element(that is ".class" here)

2
At:- 6/4/2017 11:41:16 AM
ok thank you , got it, it is working now 0
By : jaya - at :- 6/4/2017 11:43:46 AM


profileImage Answered by:- vikas_jk

You need to change your code like below to trigger on click

$(document).on('click',".Class",function(){

alert("Called in here");· 

});

to fix this issue we need to call the on method on outer elements under which we append the elements dynamically and pass jquery selector as a second argument.

 $(".Class").on('click',function(){}) one binds the event to all applicable elements present at the time of its execution and in second form it finds the applicable elements dynamically under the document object and triggers the click event.

1
At:- 6/14/2018 5:41:11 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly