Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
DOM Manipulation and Events<br />
Here, we are passing a named function instead of an anonymous function to the<br />
on() method. Let's shift our focus now to the event parameter that we pass to<br />
the function. jQuery passes an event object with all the event callbacks. An event<br />
object contains very useful information about the event being triggered. In cases<br />
where we don't want the default behavior of the element to kick in, we can use the<br />
preventDefault() method of the event object. For example, we want to fire an<br />
AJAX request instead of a complete form submission or we want to prevent the<br />
default location to be opened when a URL anchor is clicked on. In these cases, you<br />
may also want to prevent the event from bubbling up the DOM. You can stop the<br />
event propagation by calling the stopPropagation() method of the event object.<br />
Consider this example:<br />
$( "#loginform" ).on( "submit", function( event ) {<br />
// Prevent the form's default submission.<br />
event.preventDefault();<br />
// Prevent event from bubbling up DOM tree, also stops any<br />
delegation<br />
event.stopPropagation();<br />
});<br />
Apart from the event object, you also get a reference to the DOM object on which the<br />
event was fired. This element can be referred by $(this). Consider the following<br />
example:<br />
$( "a" ).click(function( event ) {<br />
var anchor = $( this );<br />
if ( anchor.attr( "href" ).match( "google" ) ) {<br />
event.preventDefault();<br />
}<br />
});<br />
Summary<br />
This chapter was all about understanding <strong>JavaScript</strong> in its most important role—<br />
that of browser language. <strong>JavaScript</strong> plays the role of introducing dynamism on the<br />
web by facilitating DOM manipulation and event management on the browser. We<br />
discussed both of these concepts with and without jQuery. As the demands of the<br />
modern web are increasing, using libraries such as jQuery is essential. These libraries<br />
significantly improve the code quality and efficiency and, at the same time, give you<br />
the freedom to focus on important things.<br />
We will focus on another incarnation of <strong>JavaScript</strong>—mainly on the server side. Node.js<br />
has become a popular <strong>JavaScript</strong> framework to write scalable server-side applications.<br />
We will take a detailed look at how we can best utilize Node.js for server applications.<br />
[ 200 ]<br />
www.it-ebooks.info