JQUERY SELECTOR, USING JQUERY TO FIND A CLASS BY ID NAME

In this section, you will learn about jQuery selectors and how to lớn find DOM element(s) using selectors.

The jQuery selector enables you lớn find DOM elements in your web page. Most of the times you will start with selector function $() in the jQuery.

Bạn đã xem: Using jquery lớn find a class by id name

The selector expression parameter specifies a pattern khổng lồ match the elements. The jQuery uses CSS selector patterns as well as its own pattern khổng lồ match the elements.

The context parameter is optional. It specifies elements in a DOM hierarchy from where jQuery starts searching for matching elements.

Let"s see commonly used selectors in jQuery.

Select Elements by Name

The most common selector pattern is element name. Specifing an element name as string e.g. $("p") will return an array of all the elements in a webpage.

The following figure shows which DOM elements will be returned from $("p") & $"(div").


Bạn đang xem: Jquery selector, using jquery to find a class by id name

*

jQuery Selectors chạy thử
*

jQuery append() method inserts text at the over in the element. You can get a particular element by using id selector pattern. Specify an id of an element for which you want to get the reference, starting with # symbol.

The following figure shows which DOM elements will be returned from $("#myDiv1") và $"(#prg2").


Xem thêm: 200+ Free Youtube To Mp4 - 200+ Free Youtube To Mp3 Converters

*

jQuery Id Selector demo

Select Elements by Attribute

jQuery also allows you to find an element based on attributes mix on it. Specifing an attribute name in square brackets in $ function e.g. $("") will return all the elements that have class attribute irrespective of value.

In the following example, jQuery returns all the elements that have class or contenteditable attribute irrespective of any value.


*

jQuery Attribute Selector You can also specify a specific value of an attribute in attribute selector. For example, $("") will return all the elements which have class attribute with myCls as a value.
Bài viết coi nhiều