This page has been robot translated, sorry for typos if any. Original content here.

JQuery

7 Scroll to Top solutions on jQuery
On the page

JQuery

Before you start working with an element, you need to learn how to select it. The selection of the elements in jQuery is built like the selection in CSS.

In order to understand how the selection of elements in jQuery takes place, let's look at the CSS file.

All that goes before braces is called a selector.

  1. Tag Name
  2. Class
  3. The identifier

All that is written in curly brackets after the selector is the properties of the element with the given selector.

What actually happens? When a user's browser accesses our CSS file, when it reaches a selector, it starts executing properties corresponding to the element with this selector. For example, in a CSS file:

 p {
  font-family: Tahoma;
  font-size: 12px;
 }

Now when the browser reaches the element

Hi

, then these properties apply to it.

Now let's see how the selection of child elements in CSS takes place. For example, we need to apply the above CSS properties not to all paragraphs, but to paragraphs with the identifier logo. Then the CSS will look like this:

 p #logo {
  font-family: Tahoma;
  font-size: 12px;
 } 

and if we want to apply these properties not only to paragraphs with id = "logo", but to all elements with id = "logo", then our CSS looks like this:

 #logo {
  font-family: Tahoma;
  font-size: 12px;
 }

We remembered how the selection of elements in CSS takes place. As I said before, the selection of elements in jQuery is done by analogy with CSS. So if we want to select all the paragraphs, then we write the following code:

  $ ("P"); 

Or if we need to select all the elements from the page with id = "logo", then write it like this:

  $ ("# Logo"); 

Similarly, we can select all the elements by class, for example class = "logo":

  $ (". Logo"); 

If we want to select child elements, for example all paragraphs

having an identifier logo, then we need to register:

  $ ("P #logo"); 

The next type of selection is selecting the next element. For example, we have the following html code:

Test 1

Test 2

And we need to select the p tag, which contains the text "Test 2". As you can see, this is not so easy, using the above samples. But in jQuery there is a special selector that allows us to simplify the task. This selector selects the next element +. Those. in our case, we can bind to a div with id = "logo" and use this selector to select the following element:

  $ ("# Logo + p"); 

Sometimes there is a situation where you need to select all the children. For this, jQuery has a selector>. For example, we have the following html code:

 <div id = "content">
  <p> Hello </ p>
  <p> We begin to study jQuery </ p>
  <p> And now we are getting acquainted with selectors of elements selection </ p>
 </ div>

And we need to select all p that are inside the div tag id = "content" Our code will look like this:

  $ ("# Content> p"); 

The following selection type in jQuery is a selection of elements by attribute values. This selection is very often found in projects, for example, to implement a drop-down menu. For example, we have the html code:

 <img src = "images / 1.png" width = "50" height = "50">
 <img src = "images / 2.png" width = "50" height = "50">
 <img src = "images / 3.png" width = "50" height = "50">

and we need to select all pictures with width = "50". Then the selection in jQuery will look like this.

  $ ("Img [width = 50]"); 

The following sampling type in jQuery is a sample of attribute values ​​that begin with a condition. For example, we have the html code:

 <img src = "images / img1.png" width = "50" height = "50">
 <img src = "images / img2.png" width = "50" height = "50">
 <img src = "images / img3.png" width = "50" height = "50">

and we need to select all the pictures whose src attribute starts with images / img, then this selection in jQuery is done like this:

  $ ("Img [src ^ = images / img]"); 

By analogy, we can make a sample of the attributes of the value, which ends with some condition. Those. from the previous html-code we want to select all png pictures. Then the selection in jQuery would look like this:

  $ ("Img [src $ =. Png]"); 

The next selection in jQuery is when you need to select an attribute whose value can be anywhere. From the previous html-code, we need to select all the pictures in the title, which have the word "img".

  $ ("Img [src * = img]"); 

We examined the basic methods for selecting elements in jQuery, which allow us to select any element, and then perform certain actions on it. For more information on jQuery selection methods, see official website . But the selection of the elements in jQuery does not end there and there are sample filters for more delicate selection of the elements in jQuery.

JQuery Element Selection Filter

In jquery, there are many methods for filtering selected items, which you can read about in the official documentation. And I will only consider the most necessary and often used (in my opinion) filters.

Select even elements even

There arises such a situation, when it is necessary to select only even elements from the table. For this, the developers have created a special filter: even. For example, we have a table:

 <table id = "'table'">
 <tbody>
 <tr>
 <td> 1 </ td>
 <td> Name 1 </ td>
 </ tr>
 <tr>
 <td> 2 </ td>
 <td> Name 2 </ td>
 </ tr>
 <tr>
 <td> 3 </ td>
 <td> Name 3 </ td>
 </ tr>
 <tr>
 <td> 4 </ td>
 <td> Name 4 </ td>
 </ tr>
 </ tbody>
 </ table>
 <pre>

And we need to select even lines. To do this, select the necessary elements and apply a parity filter to them. Here's how it looks in jQuery:

  $ ('# table tr: even'); 

So we chose all the even lines. But be careful here - all even lines from the point of view of javascript. And in javascript, the count starts at 0, so we will have the first row in the table selected and the third row in the table.

Odd element selection odd

The opposite (if I may say so) to the previous filter is the odd filter - the choice of odd elements. Suppose from the table we need to select odd rows. Then apply the odd filter and look like this:

  $ ('# table tr: odd'); 

Selecting the first element first

If we need to select only one element - the first one, then the filter first is suitable for these purposes. For example, from the table we need to select only the first row, then the selection of this element will look like this:

  $ ('# table tr: first'); 

Select last item last

To select the last element, apply the last filter. For example, from our table you need to select the last line. Then our sample:

  $ ('# table tr: last'); 

The negative filter not

This filter is necessary for the selection of all elements, except for elements that match the condition. For example, we have

 </ pre>
 <div id = "'1'"> </ div>
 <div> Example: </ div>
 <pre>

We need to select all the divs, except those divs that are inside div-a c id = '1'. And if we write:

  $ ('div'); 

Then select all the divs, even those that are in div c id = '1', so in this case you need to apply the not filter.

  $ ('div: not (# 1 div)'); 

And thus we will choose all the div needed for us

Select an element that contains the specified has element

Sometimes there are situations that it is necessary to select an element that contains within itself another specific element. And for this in jquery there is a filter has. For example, we need to select the div from the previous example, in which there is another div. Then our sample will look like this:

  $ ('div: has (div)'); 

Those. select all the div, and then apply the filter has in which we indicate that the selected div in itself must contain the other div

Selecting an element by content

For example, we need to select an element by its contents, then the contains contains a help. From the above code, we need to select the div that contains the "Example":

  $ ('div: contains (Hello)'); 

Element filter without heirs empty

There are situations where it is necessary to leave only those elements that do not have heirs and do not contain anything from the selected elements. For example, we have an html code

 </ pre>
 <table>
 <tbody>
 <tr>
 <td> 1 </ td>
 <td> </ td>
 </ tr>
 <tr>
 <td> </ td>
 <td> 2 </ td>
 </ tr>
 </ tbody>
 </ table>
 <pre>

And we need to select all td that do not have heirs.

  $ ('td: empty'); 

And as a result, we will select the second <td> from the first <tr> and the first <td> from the second <tr>.

Filter on hidden elements

Quite often in jQuery, you have to select all the hidden elements. And for this, there is a special hidden filter. Those. if we need to select all the divs that are currently hidden, then we need to write the following code:

  $ ('div: hidden'); 

Filter by visible elements

The opposite filter is the visible filter, which filters all visible elements. Those. if we need to select all the divs that are currently visible.

  $ ('div: visible'); 

JQuery Methods: text, hide, show, width, heigth, html

After selecting the required element in jQuery, we must perform actions on it. In this article, we will dwell on the actions that can be performed on elements with jQuery.

As you know, JavaScript works with the DOM model of the page. Therefore, we need JavaScript code to be executed only after building the DOM model. To do this, jQuery has a ready method that waits for the DOM model to be built.

Therefore, to run the script after building the DOM model, use the following construction:

 $ (document) .ready (function () {<script code>}) 

The text () method

The text () method is required to retrieve or change the text of an element. Suppose we have the following html code:

 <div id = 'text'>
 Hey.
 </ div>

And we want to get this text, then we need to select the div element with id = text and apply the text () method to it.

 $ (document) .ready (function () {
 $ ('# text'). text ();
 })

To change the text value of an element, we need to pass our parameter to the text method. For example, we want to change the text in the above example to "Welcome to web-programming.com", then our code will look like this:

 $ (document) .ready (function () {
 $ ('# text'). text ('Welcome to web-programming.com');
 })

The hide () method

If we need to hide the element, then in jQuery there is a method hide (speed, callback). This method can be passed two speed parameters - the disappearance rate, the time in milliseconds and the callback function that will be executed after the item disappears. For example, we need the div with id = text (from the example above) to disappear in 2 seconds, then our code will be:

 $ (document) .ready (function () {
 $ ('# text'). hide (2000);
 })

The show () method

The opposite method of hide is show, if we need to show an element. Similarly, like the hide method, show takes two parameters - the appearance speed and the function that will be executed after the element appears.

 $ (document) .ready (function () {
 $ ('# text'). show (2000);
 })

The width () method

In order to set or get the width of an element in jQuery, there is a special width method. If we need to get the width of the div element with id = text (from the example above), then we need to write the following code:

 $ (document) .ready (function () {
 $ ('# text'). width ();
 })

If we need to set the width, we need to pass the parameter to the width method. If you do not specify the units of measurement (%, em), then the default is px.

 $ (document) .ready (function () {
 $ ('# text'). width (30);
 })

The heigth () method

To change or calculate the height of an element in jQuery, use the heigth () method. This method works by analogy with the width method.

The html () method

If we want to get or change the html code of the element, then we need to use the html () method. This method works by analogy with the methods heigth () and width (). Those. if we do not pass parameters to our method, then the method will return to us all the html code that is in this element. If we pass a parameter, we change the html code in our element.

JQuery Methods: fadeOut, fadeIn, fadeTo, slideUp, slideDown

The fadeOut () method

The method fadeOut (time, function) serves for the smooth disappearance of the element (the element changes its transparency, until it disappears completely). As parameters it accepts:

  • time is the time of disappearance
  • function - the function to be executed after the element disappears

For example, we have the html code:

 <div id = 'text'>
  Hey.
 </ div>

We want the div element with id = text to disappear smoothly. To do this, write the following code:

 $ (document) .ready (function () {
  $ ('# text'). fadeOut (2000);
 })

FadeIn () method

The method fadeIn (time, function) serves for the smooth appearance of the element. The element changes its transparency to full visibility. As parameters it accepts:

  • time - time of occurrence
  • function - the function to be executed after the element disappears
 $ (document) .ready (function () {
  $ ('# text') fadeIn (2000);
 })

The fadeTo () method

Method fadeTo (time, opacity, function) - allows you to change the degree of transparency of the element to a certain value. As parameters it accepts:

  • time - change time
  • opacity - the degree of transparency from 0 to 1
  • function - the function to be executed after the element disappears

Example:

 $ (document) .ready (function () {
  $ ('# text') fadeTo (2000, 0.5);
 })

The slideUp () method

The slideUp (time, function) method allows the element to disappear by going up. As parameters it accepts:

  • time - time of occurrence
  • function - the function to be executed after the element disappears

Example:

 $ (document) .ready (function () {
  $ ('# text'). slideUp (2000);
 })

The slideDown () method

The slideDown (time, function) method allows you to display an element. The element seems to be coming down. As parameters it accepts:

  • time - time of occurrence
  • function - the function to be executed after the element disappears

Example:

 $ (document) .ready (function () {
  $ ('# text'). slideDown (2000);
 })

JQuery Methods: attr, removeAttr, addClass, removeClass, css, animate

I'll remind you that jQuery is a JavaScript framework.

In practice, it is often necessary to change various attributes of html elements, add and remove classes, view and modify various css-properties of elements. To do this, jQuery has special methods that we will cover.

Attr ()

The attr (key, value) method is used to return or change the attribute value of an element, where:

  • key - the name of the attribute
  • value - the value of the attribute (if the value is specified, it changes it, otherwise it just returns)

For example, a div with id = test has width = 300px and we want to know its width

 $ (document) .ready (function () {
  $ ('# test') attr ('width');
 })

Method removeAttr ()

The removeAttr (key) method removes the specified attribute, where:

  • key-name of the attribute

For example, we have a div with id = test with width = 300px and we want to delete this attribute:

 $ (document) .ready (function () {
  $ ('# test'). removeAttr ('width');
 })

The addClass () method

The addClass (name) method adds a class to the selected element named name. For example, add the example class to the div with id = test:

 $ (document) .ready (function () {
  $ ('# test'). addClass ('example');
 })

The removeClass () method

The removeClass (name) method removes a class from an element named name.

For example, we have a div with id = test and, we want to delete our class from this div:

 $ (document) .ready (function () {
  $ ('# test'). removeClass ('example');
 })

The css () method

The css (name, value) method allows you to get the css property of an element with the name name (if the value of this property is not specified by the second parameter) or change the css value of the property named name to value (if the second value parameter is set) For example, we have the headers h1 and we want to change the font size to 20px, then:

 $ (document) .ready (function () {
  $ ('h1'). css ('font-size', 20);
 })

The animate () method

So far we have considered a simple change to the properties of elements, but in jQuery there is another wonderful method that allows you to change the properties of an element smoothly, as if animating it. This is the animate method (settings, time, function), where:

  • setting - properties that will change their value
  • time - the time for which these properties will change the value
  • function - a function that will start its execution after the change occurs.

For example, we have a div with id = test and width = 1000px. We want to change the width of this div up to 500px in 4 seconds

 $ (document) .ready (function () {
  $ ('# test'). animate ({'width': '500px'}, 4000);
 })