I create my Html elements using java script createElement().But I cannot select any of the html elements in JQuery -- $("p").on("click",function(){}) . It works for $(document) though. My JQuery script is at the end of the body in the html page. But when inspect elements is checked in the browser after page creation html elements are below the scripts.
I have seen there many other similar questions but none of them worked. Please help me solve this I have been working on this for couple of days now and its taking me no where.
2 Answers 2
For dynamically created elements use:
$(document).on('click', 'p', function(){
console.log("clicked");
});
jsFiddle demo, showing assigning the click event before creating the element.
1 Comment
use live instead of on.
$("p").live("click",function(){}
Edit:
use of live is deprecated we can use on but by working from thee document, and not from an element.
Use:
$(document).on( 'click', '.someClass', doSomething);
Instead of:
$('.someClassParent').on( 'click', '.someClass', doSomething);
5 Comments
live has already been removed from the jQuery core in the latter versions, and shouldn't be used since it was deprecated. A direct equivalent to live is the on variation with the descendant selector as MrCode answered. Ref: stackoverflow.com/questions/8021436/… jquery.com/upgrade-guide/1.9/#live-removed api.jquery.com/live .on was implemented.$(document).on( instead $("p").on(, we can work from the document, and not from an element. forum.jquery.com/topic/…