``` > Be extra-sure to include your custom `script.js` file **after** the inclusion of the jQuery file. --- ### Test everything .breadcrumbs[jQuery - DOM Manipulation > Include jQuery] Add the following line in your `script.js` file, and save it: ```js console.log($("body").jquery); ``` Start your project with `live-server` and access your browser console. You should see the following lines: ```bash 3.1.1 Live reload enabled. ``` > If it's the case, you're good to go. jQuery and your custom script are both correctly include in your project. --- class: center, middle ## jQuery documentation .breadcrumbs[jQuery - DOM Manipulation] Everything that is presented in this slide-deck can also be found in **the jQuery documentation**, along with lot of **examples** and **information**. We highly recommend that you check it out. [jQuery Documentation][jq-doc] --- ## The `$` object .breadcrumbs[jQuery - DOM Manipulation] The complete jQuery library is accessible in your JS code through the use of the global `$` variable. > Some other libraries also offers a `$` variable as there main entry-point. That could be the cause for conflict between jQuery and those librairies. If it's the case, you can use the global `jQuery` variable instead. To be sure to remove all possible conflicts, you can use the special `.noConflict()` jQuery method at the top of your JS file: ```js $.noConflict(); // Code that uses other library's $ can follow here. // But use the jQuery variable to access jQuery's method. ``` > This shouldn't be the case in this course. --- ## Selecting things .breadcrumbs[jQuery - DOM Manipulation] Being a library designed to easily handle DOM manipulation, jQuery allows you to... easily select DOM elements. The selection functionnality is **quite broad and powerful**, and is based on the **same syntax as [CSS selectors][css-select]**. To select DOM elements and receive jQuery objects matching the selected elements, use the `$()` function, passing it a selector as parameter: | Selector | CSS example | jQuery | Result | | :------- | :----- | :---------- | :----- | | Element | `p` | `$("p")` | **All** `
` elements in the page | | Id | `#username` | `$("#username")` | **Unique** element with the `username` id | | Class | `.row` | `$(".row")` | **All** elements with a `row` class | > Using a jQuery selector will always return you an `array` of jQuery objects, even with an Id selector (that should return one element). --- ### Good selecting practice .breadcrumbs[jQuery - DOM Manipulation > Selecting things] Selecting elements in jQuery being [very vast][css-select], you can easily write selectors that aren't optimals, performace-wise. > Traversing the DOM is a **costly operation**. Your jQuery selectors (and your CSS ones as well) should try to be **as to-the-point as possible**. There's many good practice regarding jQuery selectors. We are only going to see a few of them. ---- > Note that the following example contains some jQuery method. > > **Don't mind them now, we will explicit these methods later on the course.** --- #### Storing jQuery object .breadcrumbs[jQuery - DOM Manipulation > Selecting things > Good selecting practice] When you know or discover that your are going to use the same jQuery selector **several time**, you should cache its result **in a JS variable**, for future reference. **Bad!** ```js $("article > p:first-child").addClass("catch-phrase"); $("article > p:first-child").text("This is a very pertinent article"); $("article > p:first-child").append("CLICK ME!"); ``` **Way better!** ```js var `$firstSentences` = $("article > p:first-child"); `$firstSentences`.addClass("catch-phrase"); `$firstSentences`.text("This is a very pertinent article"); `$firstSentences`.append("CLICK ME!"); ``` > For better code-reading, it's a good practice to **precede** caching variable's name with **the `$` character**, to indicate that **it contains jQuery object(s)**. --- #### Use `id` selectors .breadcrumbs[jQuery - DOM Manipulation > Selecting things > Good selecting practice] HTML `id` attribute allows you to define **unique identifier** in an HTML page. Thanks to this uniqueness, element with `id` attributes are **extremly fast to retrieve** in the DOM, even with older browsers. > As much as possible, you should add `id` attributes to HTML elements that you'll select. ```html
``` This... ```js $("div.panel-body p:nth-of-type(2)") ``` ...will be way **slower** than this. ```js $("#the-one") ``` --- #### Don't over do it .breadcrumbs[jQuery - DOM Manipulation > Selecting things > Good selecting practice] If you can't define `id` attributes in your HTML template, you'll need to use more complete selectors. When doing so, you might be tempted to be **as exhaustive as possible**, in order to be sure you'll get the right element(s). Like this for example: ```js $("html body main.container div.list-group a.list-group-item h4") ``` > This is completely **unnecessary** and a **waste of resources**. You need to be precise with your selector to avoir selecting things you don't want. But being over-precise is **as bad** as being too vague. > Knowing your HTML structure is **mandatory** when writing jQuery selectors. We could simplify the precedent example like this: ```js $("a.list-group-item h4") ``` --- #### Using `class` selector .breadcrumbs[jQuery - DOM Manipulation > Selecting things > Good selecting practice] CSS classes being shared among elements, it's a great way to quickly select all related elements. ```js // Will select all items from list-group lists. $(".list-group-item") ``` But there's a drawback. With `class` selector, jQuery will parse **all the DOM** and test **every single node** to see if it has the given class (or classes). This is possibly very inefficient. Thus, you should try to be **as precise as possible** when using `class` selector, by qualifying it with a tag name, for example. In our example, we know that the `.list-group-item` should only be applied to `