296 CHAPTER 6 Essential JavaScript and jQuery
It’s best to place this at the bottom of your HTML document and call an initialize function
that contains all initialization code.
Lesson summary
■■Download jQuery from http://jQuery.com or install it from the NuGet package
manager.
■■The jQuery library is in a jQuery namespace and is aliased as a dollar sign ($).
■■Use the $(selector) syntax to locate document object model (DOM) elements. The
result of $(selector) is a jQuery wrapper object containing zero to many DOM elements
that match the selector. You can use the length property to find out whether there are
any matches to the selector.
■■Use jQuery’s val method to get or set the value of a DOM element that has a value
property.
■■To enable IntelliSense, create a _references.js file in the Scripts folder and add library
references to this file. In your JavaScript files, add a reference to the _references.js file.
■■Use jQuery’s .on and .off methods to add and remove event listeners.
■■Use the $(document).ready(function(){ initialize( ); }); expression to add initialization
code.
Lesson review
Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.
- You want to locate all the elements on your webpage that are assigned the CSS class
name Hidden. Which jQuery statement can you use?
A. var hidden = $ (‘#Hidden’);
B. var hidden = $ (‘.Hidden’);
C. var hidden = $ (‘Hidden’);
D. var hidden = $(‘class=Hidden’); - You are interested in writing event-driven JavaScript code that will work on most
browsers without writing browser-specific code. How can you accomplish this?
A. Use the jQuery library to help.
B. Use only JavaScript statements that are the same across all browsers.
C. Do not use any JavaScript.
D. It’s impossible to write event-driven JavaScript code that is not browser-specific.