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.

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

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

The previous content of the paragraph is removed, <strong>and</strong> the new text is inserted. Note that the span<br />

tag was removed as well; all contents of an element are replaced when using .text() <strong>and</strong> .html().<br />

To insert HTML into the paragraph, replace its contents again with the following snippet:<br />

$("#bar").html("This is some HTML text.");<br />

After execution, the new text appears in the paragraph <strong>and</strong> the word “HTML” appears in bold (see<br />

Figure 2-16).<br />

Figure 2-16. The browser after inserting text <strong>and</strong> HTML tags<br />

.val()<br />

Accessing <strong>and</strong> modifying the content of form elements is accomplished through the .val() method. This<br />

method returns the value of an input, or if a value is supplied, sets the value of an input.<br />

Retrieve the value of the submit button in your test form using the following:<br />

$(":submit").val();<br />

which outputs this:<br />

>>> $(":submit").val();<br />

"Log In"<br />

Now, update the value of the submit input to read "Sign In" using this code:<br />

57

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

Saved successfully!

Ooh no, something went wrong!