Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning jQuery 3 - Fifth Edition

You're reading from  Learning jQuery 3 - Fifth Edition

Product type Book
Published in May 2017
Publisher Packt
ISBN-13 9781785882982
Pages 448 pages
Edition 5th Edition
Languages
Authors (2):
Jonathan Chaffer Jonathan Chaffer
Profile icon Jonathan Chaffer
Karl Swedberg Karl Swedberg
Profile icon Karl Swedberg
View More author details
Toc

Table of Contents (23) Chapters close

Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface
1. Getting Started 2. Selecting Elements 3. Handling Events 4. Styling and Animating 5. Manipulating the DOM 6. Sending Data with Ajax 7. Using Plugins 8. Developing Plugins 9. Advanced Selectors and Traversing 10. Advanced Events 11. Advanced Effects 12. Advanced DOM Manipulation 13. Advanced Ajax 14. Testing JavaScript with QUnit 15. Quick Reference

Using development tools


As this code comparison has shown, jQuery code is typically shorter and clearer than its basic JavaScript equivalent. However, this doesn't mean we will always write code that is free from bugs or that we will intuitively understand what is happening on our pages at all times. Our jQuery coding experience will be much smoother with the assistance of standard development tools.

High-quality development tools are available in all modern browsers. We can feel free to use the environment that is most comfortable to us. Options include the following:

  • Microsoft Edge (https://developer.microsoft.com/en-us/microsoft-edge/platform/documentation/f12-devtools-guide/)
  • Internet Explorer Developer Tools (http://msdn.microsoft.com/en-us/library/dd565628.aspx)
  • Safari Web Development Tools (https://developer.apple.com/safari/tools/)
  • Chrome Developer Tools (https://developer.chrome.com/devtools)
  • Firefox Developer Tools (https://developer.mozilla.org/en-US/docs/Tools)

Each of these toolkits offers similar development features, including:

  • Exploring and modifying aspects of the DOM
  • Investigating the relationship between CSS and its effect on page presentation
  • Convenient tracing of script execution through special methods
  • Pausing execution of running scripts and inspecting variable values

While the details of these features vary from one tool to the next, the general concepts remain the same. In this book, some examples will require the use of one of these toolkits; we will use Chrome Developer Tools for these demonstrations, but development tools for other browsers are fine alternatives.

Chrome Developer Tools

Up-to-date instructions for accessing and using Chrome Developer Tools can be found on the project's documentation pages at https://developer.chrome.com/devtools. The tools are too involved to explore in great detail here, but a survey of some of the most relevant features will be useful to us.

Note

Understanding these screenshotsChrome Developer Tools is a quickly evolving project, so the following screenshots may not exactly match your environment.

When Chrome Developer Tools is activated, a new panel appears offering information about the current page. In the default Elements tab of this panel, we can see a representation of the page structure on the left-hand side and details of the selected element (such as the CSS rules that apply to it) on the right-hand side. This tab is especially useful for investigating the structure of the page and debugging CSS issues:

The Sourcestab allows us to view the contents of all loaded scripts on the page. By right-clicking on a line number, we can set a breakpoint, set a conditional breakpoint, or have the script continue to that line after another breakpoint is reached. Breakpoints are effective ways to pause the execution of a script and examine what occurs in a step-by-step fashion. On the right-hand side of the page, we can enter a list of variables and expressions we wish to know the value of at any time:

The Console tab will be of most frequent use to us while learning jQuery. A field at the bottom of the panel allows us to enter any JavaScript statement, and the result of the statement is then presented in the panel.

In this example, we perform the same jQuery selector as in Listing 1.2, but we are not performing any action on the selected elements. Even so, the statement gives us interesting information: we see that the result of the selector is a jQuery object pointing to the two .poem-stanza elements on the page. We can use this console feature to quickly try out jQuery code at any time, right from within the browser:

In addition, we can interact with this console directly from our code using the console.log() method:

$(() => {
  console.log('hello');
  console.log(52);
  console.log($('div.poem-stanza'));
});

Listing 1.4

This code illustrates that we can pass any kind of expression into the console.log() method. Simple values such as strings and numbers are printed directly, and more complicated values such as jQuery objects are nicely formatted for our inspection:

This console.log() function (which works in each of the browser developer tools we mentioned earlier) is a convenient alternative to the JavaScript alert() function, and will be very useful as we test our jQuery code.

You have been reading a chapter from
Learning jQuery 3 - Fifth Edition
Published in: May 2017 Publisher: Packt ISBN-13: 9781785882982
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 €14.99/month. Cancel anytime