02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>DOM</strong> Manipulation <strong>Methods</strong><br />

Description<br />

The .before and .insertBefore methods perform the same task. The only<br />

difference is in the syntax—specifically, in the placement of the content and target.<br />

With .before(), the selector expression preceding the method is the container into<br />

which the content is inserted. With .insertBefore(), on the other hand, the content<br />

precedes the method, either as a selector expression or as markup created on the fly,<br />

and it is inserted before the target container.<br />

Consider the following HTML:<br />

<br />

Demonstration Box<br />

<br />

<br />

The two s, with a little CSS, are rendered on the right side of the page<br />

as follows:<br />

We can insert an HTML structure before like so:<br />

$('div.demo-box').before('This text<br />

was inserted');<br />

The new and elements, as well as the text nodes, are created on<br />

the fly and added to the <strong>DOM</strong>. The result is a new positioned outside of, just<br />

before, :<br />

[ 82 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!