0

Check If An Element Exists Using jQuery

posted on November 04, 2011 01:14 AM by Jhoy Imperial
under jQuery, Snippets

Here are some examples on how to determine if an element is existing in your html page.

jQuery selector sample:


if ($("yourElementSelector").length) {

//your script here

}

jQuery selector sample 2:


if (jQuery("yourElementSelector").length) {

//your script here

}

if ($(".elementClassName").length) {

//your script here

}

if ($("#elementIdName").length) {

//your script here

}

HTML tag selector sample:


if ($("ul").length) {

//your script here

}

if ($("ul").length) {

//your script here

}

Check if element exists using plain javascript, select the element using its id=”" attribute


if (document.getElementById("elementId") != 'undefined') {

//your script here

}

Hope this will be useful to anyone :)

Recent Tweets

Sponsored Links

Recent Comments

 

"Minimalist Cereal 2"
design and theme by Jhoy Imperial

Email us at support@codingcereal.com

Copyright 2010 - 2011 © www.codingcereal.com