03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

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

406 x CHAPTER 11 JQUERY<br />

jQuery goes one step further, <strong>and</strong> enables you to hook up events not only to a s<strong>in</strong>gle element, but to<br />

a whole matched set at once. This is extremely powerful because it enables you to b<strong>in</strong>d h<strong>and</strong>lers to<br />

a large number of elements with only a few l<strong>in</strong>es of code. Consider, for example, a table with many<br />

rows. To make the table a little more visually attractive, you could apply a technique that is called<br />

“active item track<strong>in</strong>g,” where the item your mouse is over changes color. Without jQuery you would<br />

write onmouseover <strong>and</strong> onmouseout h<strong>and</strong>lers on each <strong>and</strong> every row <strong>in</strong> the table. This clearly leads<br />

to a lot of excessive bloat <strong>in</strong> the f<strong>in</strong>al HTML of the page. With jQuery, all you need is this code,<br />

aga<strong>in</strong> us<strong>in</strong>g the HTML code samples you used before:<br />

$(function()<br />

{<br />

$('#DemoTable tr')<br />

.b<strong>in</strong>d('mouseover', function() { $(this).css('background-color', 'yellow') });<br />

});<br />

This code f<strong>in</strong>ds all the table rows with<strong>in</strong> the #DemoTable element <strong>and</strong> then dynamically assigns a<br />

function that is called when you hover your mouse over each row. If you hover your mouse over<br />

them, the background changes color. But if you move your mouse away, the new color rema<strong>in</strong>s.<br />

To fix this problem, you can use jQuery’s cha<strong>in</strong><strong>in</strong>g concept, where the result of a jQuery method<br />

returns the matched set so you can apply another function to it. To b<strong>in</strong>d the mouseout to a new<br />

function, simply call b<strong>in</strong>d aga<strong>in</strong> on the return value of the first call to b<strong>in</strong>d:<br />

$('#DemoTable tr')<br />

.b<strong>in</strong>d('mouseover', function() { $(this).css('background-color', 'yellow') })<br />

.b<strong>in</strong>d('mouseout', function() { $(this).css('background-color', '') });<br />

Notice how the semicolon that closed off the l<strong>in</strong>e <strong>in</strong> the previous example has been moved to the<br />

f<strong>in</strong>al l<strong>in</strong>e. Then the second b<strong>in</strong>d is simply cha<strong>in</strong>ed to the previous call to b<strong>in</strong>d. It’s a bit difficult to<br />

see because the code is spread out over multiple l<strong>in</strong>es to accommodate the width of this book, but<br />

this code actually comes down to this:<br />

$('#DemoTable tr').b<strong>in</strong>d('mouseover', ...). b<strong>in</strong>d('mouseout', ...);<br />

This code does three th<strong>in</strong>gs: first it uses $('#DemoTable tr') to f<strong>in</strong>d all rows <strong>in</strong> the table. On the<br />

matched set that is returned it calls b<strong>in</strong>d to dynamically hook up some behavior that fires when you<br />

move your mouse over a row. Then b<strong>in</strong>d is called aga<strong>in</strong> on the matched set returned by the first call<br />

to b<strong>in</strong>d to reset the background color when you move your mouse away from the table row. Notice<br />

how I am sett<strong>in</strong>g the color to an empty str<strong>in</strong>g ('') to remove the CSS background property so you can<br />

see the orig<strong>in</strong>al background aga<strong>in</strong>.<br />

There’s one more important th<strong>in</strong>g to look at <strong>in</strong> this example <strong>and</strong> that’s the way the background<br />

color is set:<br />

$(this).css('background-color', 'yellow')<br />

The this keyword <strong>in</strong> this example refers to the element to which the item is applied: the table row <strong>in</strong><br />

this case. Us<strong>in</strong>g $(this) then gives you a jQuery matched set (conta<strong>in</strong><strong>in</strong>g a s<strong>in</strong>gle element) to which<br />

you can apply regular jQuery methods such as css. Instead of us<strong>in</strong>g jQuery you can also execute<br />

st<strong>and</strong>ard JavaScript aga<strong>in</strong>st the this element.<br />

this.style.backgroundColor = 'yellow'

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

Saved successfully!

Ooh no, something went wrong!