Posted By

noah on 05/04/07


Tagged

check test DOM boolean boundary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

korzhik


Does an element exist?


 / Published in: JavaScript
 

Does an element exist in the current DOM tree?

Because I can never remember whether I should test typeof el == null... or el == null

  1. <div id="bar" />
  2. <script>
  3. if (document.getElementById('foo') == null) {
  4. alert('foo is missing!');
  5. }
  6. //or:
  7. if ( ! document.getElementById('bar')) {
  8. alert('bar is missing!');
  9. }
  10. </script>

Report this snippet  

You need to login to post a comment.