Get Started with jQuery: First Examples
- 2.1. Accessing Elements and Protecting the DOM
- 2.2. Editing the Web Page with DHTML ? la jQuery
- 2.3. Animatedly Reducing and Enlarging of an Element
- 2.4. Changing Attributes Dynamically
Read Learning jQuery: A Hands-on Guide to Building Rich Interactive Web Front Ends and more than 24,000 other books and videos on Safari Books Online. Start a free trial today.
In this chapter, we make first contact with jQuery without any further preparations.In other words, we are jumping right into the deep end. I am anxious for you to get a feeling for what you can do with jQuery and what you can get out of this framework. Just accept for now that many questions regarding the source text have to remain open at this stage. Don’t worry, though; these questions are answered over the next few chapters. The explanations on the listings also remain somewhat superficial at this stage, to avoid going off topic. We want to get into the practical application of jQuery as quickly as possible and just have some fun playing around, which means creating examples.
2.1. Accessing Elements and Protecting the DOM
The following example demonstrates another extremely important function of jQuery—protecting the DOM. More on what this is all about later. For now, let’s just say that different browsers process the web page differently on loading (parsing) the page, which can lead to a number of problems when the elements of the web page are accessed (especially if you try to access the elements of the web page too soon in a script—in other words, before the browser has correctly constructed the DOM). Here, jQuery offers a reliably method for mastering these problems.
The example also shows you in passing, as it were, how you can use jQuery as a standardized way of accessing contents of elements with text and reacting to events. But enough introduction. Here is our very first listing (ch2_1.html):3
Listing 2.1. The First jQuery Example
Just create the HTML file in a separate directory and save it under the listed name.
In practice, you would usually save all your resources that are part of a project within a separate directory. For a web project, the best solution is to create these directories in the shared folder of your web server. In the case of Apache/XAMPP, this is usually the directory htdocs. This has the advantage that—if the web server is running—you can run the test directly via HTTP and a proper web call, not just load the file via the FILE protocol into the browser (in other words, the classic opening as file or simply dragging the file into the browser). The latter is not a realistic, practice-related test because later the pages also have to be requested by the visitor via a web server.
If you are working with an integrated development environment (IDE) such as Aptana or the Visual Studio Web Developer, you can usually display a web page directly from the IDE via an integrated web server. In Aptana, this is done via the Run command, and in Web Developer (a Firefox add-on) you can use the shortcut Ctrl+F5.
Figure 2.1. In this project, the jQuery library is located in the directory lib, seen from the perspective of the website.
Let’s now take a quick look at the method ready() that starts in line 10 and goes up to line 22. This method ensures that the calls it contains are only executed when the web page has been fully loaded and the DOM is correctly constructed. As hinted at before and without going into too much detail, this is already a feature whose value cannot be appreciated highly enough.
Within the ready() method, three event handlers each specify the reaction when clicking the listed elements. In our examples, these are three buttons marked with a unique ID.
The allocation to the correct function is achieved via the ID and triggering the function within the method click(). Note that we are using an anonymous function here (without an identifier).
It also gets interesting if a user clicks one of the buttons. This displays a specific text output in a section of the web page. We are again using $() and an ID for selecting the section (a div block) and the method html() for accessing the content.
Figure 2.2. The web page with the three buttons; the user has just clicked the third button.