jquery check for element existence in the DOM

Select the element

If you’re familiar with jQuery, which I suppose is true, you know how to select an element, for instance if there’s an element with id element_id, you do:

$('#element_id')

If the element exists in the DOM

if ( $('#element_id').length > 0 )
       console.log('the element with element_id
                    exists in the DOM');

Note: that console.log is present as console writing function under FF and Safari only.

if not …

Than $(‘#element_id’).length equals to 0

if ( $('#element_id').length == 0 )
       console.log('the element with element_id
                    don't exists in the DOM');

Related posts:

  1. jQuery check for element visibility
  2. Remove DOM Element with JQuery
  3. mouseclick outside a DOM element
This entry was posted in javascript, micro tutorial and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">