Reputation: 53
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.
Upvotes: 3
Views: 5679
Reputation: 1488
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);
Upvotes: 0
Reputation: 64526
For dynamically created elements use:
$(document).on('click', 'p', function(){
console.log("clicked");
});
jsFiddle demo, showing assigning the click event before creating the element.
Upvotes: 11