02.06.2013 Views

jQuery in Action - Manning Publications

jQuery in Action - Manning Publications

jQuery in Action - Manning Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

10 CHAPTER 1 Introduc<strong>in</strong>g <strong>jQuery</strong><br />

$("#someElement").html("I have added some text to an element");<br />

and<br />

$("#someElement")[0].<strong>in</strong>nerHTML =<br />

"I have added some text to an element";<br />

Because we’ve used an ID selector, only one element will match the selector. The first<br />

example uses the <strong>jQuery</strong> html() method, which replaces the contents of a DOM element<br />

with some HTML markup. The second example uses <strong>jQuery</strong> to retrieve an array<br />

of elements, selects the first one us<strong>in</strong>g an array <strong>in</strong>dex of 0, and replaces the contents<br />

us<strong>in</strong>g an ord<strong>in</strong>ary JavaScript property assignment to <strong>in</strong>nerHTML.<br />

If we wanted to achieve the same results with a selector that results <strong>in</strong> multiple<br />

matched elements, the follow<strong>in</strong>g two fragments would produce identical results<br />

(though the latter example is not a recommended way of cod<strong>in</strong>g us<strong>in</strong>g <strong>jQuery</strong>):<br />

$("div.fillMeIn")<br />

.html("I have added some text to a group of nodes");<br />

and<br />

var elements = $("div.fillMeIn");<br />

for(var i=0;i

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

Saved successfully!

Ooh no, something went wrong!