How can I determine if an element exists on a page... for instance...
$('select[name="modifier_option"]')
If that select box exists on the screen I need to validate it's value on the page to ensure it's value is > 0, but if it doesn't exist I don't need to worry about it.
Approach 1: First, we will use document. getElementById() to get the ID and store the ID into a variable. Then compare the element (variable that store ID) with 'null' and identify whether the element exists or not.
jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".
if( $('select[name="modifier_option"]').length )
{
// it exists
}
copy/paste from here: Is there an "exists" function for jQuery?
jQuery.fn.exists = function(){return jQuery(this).length>0;}
if ($(selector).exists()) {
// Do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With