javascript - function gets called even when the ID is not present in the document -


i have following javascript:

 $(document).ready(function () {     ...     $("#myselector").ready(function () {         window.alert('what hell!!!');     });  }); 

i expect pop window appear every time myselector appears somewhere in document.

the problem facing right code running (i pop up) when selector not exist in document.

why happening?

ready function executed when dom ready, ready event fired when don't pass arguments jquery, ie $().ready(), ignores selector. can use length property:

$(document).ready(function () {     if ( $("#myselector").length ) {        // ...     } }); 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -