02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

56<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

■ Tip The values returned are CSS shorth<strong>and</strong> properties. 3<br />

An added bonus of <strong>jQuery</strong> is the ability to set CSS<br />

properties using CSS shorth<strong>and</strong>, which doesn't work using basic JavaScript.<br />

.text() <strong>and</strong> .html()<br />

When dealing with the contents of an element, the .text() <strong>and</strong> .html() methods are used. The<br />

difference between the two is that .html() will allow you to read out <strong>and</strong> insert new HTML tags into an<br />

element, where .text() is for reading <strong>and</strong> writing text only.<br />

If either of these methods is called on an element set with no arguments, the contents of the<br />

element are returned. When a value is passed to the method, the existing value is overwritten, <strong>and</strong> the<br />

new one put in its place.<br />

To read the text out of the paragraph with ID bar, run the following code in the console:<br />

$("#bar").text();<br />

This captures all text (including whitespace) but ignores the span tag. The following is output:<br />

>>> $("#bar").text();<br />

"Paragraph with an id.<br />

"<br />

And this sentence is in a span.<br />

To read everything out of the paragraph, including the span tag, use the following code:<br />

$("#bar").html();<br />

This results in the following:<br />

>>> $("#bar").html();<br />

"Paragraph with an id.<br />

"<br />

And this sentence is in a span.<br />

Now, change the text <strong>by</strong> passing a value to the .text() method:<br />

3 http://www.456bereastreet.com/archive/200502/efficient_css_with_shorth<strong>and</strong>_properties/

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

Saved successfully!

Ooh no, something went wrong!