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.

240<br />

CHAPTER 7 ■ ENHANCING THE USER INTERFACE WITH JQUERY<br />

Creating a Modal Window for Event Data<br />

The modal window you’ll create for this app will be fairly simple; the script to create it will follow these<br />

steps:<br />

1. Prevent the default action (opening the detailed event view in view.php).<br />

2. Add an active class to the event link in the calendar.<br />

3. Extract the query string from the event link’s href attribute.<br />

4. Create a button that will close the modal window when clicked.<br />

5. Create the modal window itself <strong>and</strong> put the Close button inside it.<br />

6. Use AJAX to retrieve the information from the database <strong>and</strong> display it in the<br />

modal window.<br />

All of the preceding steps will be carried out when the click event is fired for an event title link.<br />

Binding a Function to the Click Event of Title Links<br />

Begin <strong>by</strong> adding a new selector to init.js that selects all anchor elements that are direct descendants of<br />

list items (li>a) <strong>and</strong> use the .live() method to bind a h<strong>and</strong>ler to the click event. Insert the following<br />

bold code into init.js:<br />

// Makes sure the document is ready before executing scripts<br />

<strong>jQuery</strong>(function($){<br />

// Pulls up events in a modal window<br />

$("li>a").live("click", function(event){<br />

});<br />

});<br />

// Event h<strong>and</strong>ler scripts go here<br />

Preventing the Default Action <strong>and</strong> Adding an Active Class<br />

Next, you need to prevent the default action <strong>by</strong> using .preventDefault(), then add an active class to the<br />

clicked element using .addClass().<br />

This is accomplished <strong>by</strong> adding the following bold code:<br />

// Makes sure the document is ready before executing scripts<br />

<strong>jQuery</strong>(function($){<br />

// Pulls up events in a modal window<br />

$("li>a").live("click", function(event){<br />

// Stops the link from loading view.php

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

Saved successfully!

Ooh no, something went wrong!