Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Web Development

You're reading from   Practical Web Development Learn CSS, JavaScript, PHP, and more with this vital guide to modern web development

Arrow left icon
Product type Paperback
Published in Jul 2015
Publisher
ISBN-13 9781782175919
Length 276 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Paul Wellens Paul Wellens
Author Profile Icon Paul Wellens
Paul Wellens
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. The World Wide Web FREE CHAPTER 2. HTML 3. CSS 4. JavaScript 5. PHP 6. PHP and MySQL 7. jQuery 8. Ajax 9. The History API – Not Forgetting Where We Are 10. XML and JSON 11. MongoDB 12. Mobile First, Responsive Design with Progressive Enhancement 13. Foundation – A Responsive CSS/JavaScript Framework 14. Node.js A. Bootstrap – An Alternative to Foundation Index

Event handlers and jQuery


Congratulations, you have reached a major milestone in the book. This page introduces a few new concepts all at once and you are going to use a lot of them daily. Let's assume that you are building a website with a menu. The menu is built using an ordered list, and here is the code for one menu item:

<li id="intnews"><a href="oldsite/intnews.php" class="news">International</a></li>

In your custom JavaScript file, you have:

$("#mainmenu").on("click", "a.news", function(e){

e.preventDefault();

var nav = $(this).parent().attr("id");

updateNewsContent(nav);

});

Let's first discuss the .on() part. This is the jQuery way of performing event handling. An event occurs when the visitor of your site performs a certain action. A typical action is the click of a mouse on a button or a link. We can then catch that event and perform certain tasks inside the function that is our event handler. In the previous example, we perform them all in a function...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime