Welcome to WebmasterWorld Guest from 54.225.32.164

Forum Moderators: open

jQuery, run only if element exists

.on('load',...) or .length

     
1:04 am on Jun 30, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 826
votes: 71


I only want a function to run if an element exists on the page... which would you guys do, and why?

if ($(element).length) {
// do this
}

$(element)
.on('load', function() {
// do this
});