Check the existence of an element in jquery

When dealing with dynamic creation of DOM objects, sometimes you need to check if a certain element exists. Below is how you check:

if ($('#my_element').length > 0){
  // it exists!
}

If you need to check whether a dropdown has any options, here is how

if ($('#my_dropdown option').length > 0){
  // contains options
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s