Tag Archives: DOM

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');

mouseclick outside a DOM element

What if you have to close on outside mouse click?

This situation is kind to be familiar. There is some panel which should be closed on outside click. Yeah, this is the most common usage of the outside click. And to do that is pretty simple.

In this tutorial … jQuery

For those of you not used to use jQuery, in short this is a JavaScript library, which gives you the power to manipulate the DOM with ease. One of the very good parts of jQuery is really the ability to help you with less code to do the hard work of searching and changing the DOM. If you don’t have any experience with this library, well you can use any other if you’d like.

First step: combine the events

One of the events should be click over the panel, and the other click over the document. Continue reading mouseclick outside a DOM element

Remove DOM Element with JQuery

JQuery and DOM

JQuery is one of the most famous JavaScript library online. There are so much articles in the web about it, that I feel useless to describe it again. The good parts of that library is the amazing way it handles the DOM. It is so good at that that I usually use it with other JavaScript libraries.

Now I’m working on a task and one of the main target of the completed task was the RAM usage of the code. It just has about 100 dynamic DOM elements which on every event should be removed form the tree and the RAM should be freed. Continue reading Remove DOM Element with JQuery