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
0