02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 83 ]<br />

Chapter 4<br />

An element (or array of elements) that already exists on the page could be moved to<br />

the <strong>DOM</strong> position just before as well. The following code,<br />

for example, moves the document's first paragraph by using a jQuery object:<br />

$('div.demo-box').before( $('p:eq(0)') );<br />

.insertBefore()<br />

Inserts every element in the set of matched elements before the set of elements<br />

specified in the parameter.<br />

.insertBefore(content)<br />

Parameters<br />

•<br />

content: A selector or element before which the matched set of elements will<br />

be inserted<br />

Return Value<br />

The jQuery object, for chaining purposes.<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 />

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

Saved successfully!

Ooh no, something went wrong!