Snippets, Tutorials

How to check if div exists in jQuery

This is one of the most commonly used piece of code for jQuery. Often times, we need to check if a div/element exists in the page before processing a block of script (eg: replacing the content of the div).

if ( $("#message").length ) {
  $('#message').text("Hello!");
}
Career Guide for Software Developers