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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Add a Close Button<br />

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

As it st<strong>and</strong>s right now, the only way to get rid of the modal window after clicking an event title is to<br />

reload the page. Of course, this isn’t good enough, so you need to add a Close button.<br />

To accomplish this, you need to create a new link <strong>and</strong> bind a click event h<strong>and</strong>ler to it that removes<br />

the modal window from the DOM. To give it a traditional Close button feel, use the multiplication<br />

symbol as its content (<strong>and</strong> the CSS in ajax.css adds the word "close" in front of it). Also, add an href<br />

attribute to make sure hovering over the link causes the mouse to behave as though the button is<br />

clickable.<br />

Next, add a Close button <strong>by</strong> inserting the following bold code into init.js:<br />

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

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

// Stops the link from loading view.php<br />

event.preventDefault();<br />

// Adds an "active" class to the link<br />

$(this).addClass("active");<br />

// Gets the query string from the link href<br />

var data = $(this)<br />

.attr("href")<br />

.replace(/.+?\?(.*)$/, "$1"),<br />

// Checks if the modal window exists <strong>and</strong><br />

// selects it, or creates a new one<br />

modal = fx.initModal();<br />

// Creates a button to close the window<br />

$("")<br />

.attr("href", "#")<br />

.addClass("modal-close-btn")<br />

.html("&times;")<br />

.click(function(event){<br />

// Prevent the default action<br />

event.preventDefault();<br />

// Removes modal window<br />

$(".modal-window")<br />

.remove();<br />

})<br />

.appendTo(modal);<br />

// Loads the event data from the DB<br />

$.ajax({<br />

type: "POST",<br />

url: processFile,<br />

data: "action=event_view&" + data,<br />

success: function(data){<br />

// Alert event data for now<br />

253

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

Saved successfully!

Ooh no, something went wrong!