Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery selector

jQuery selector


jQuery selectors allow you to manipulate groups of HTML elements or individual elements.


jQuery selector

jQuery selectors allow you to manipulate groups of HTML elements or individual elements.

jQuery selectors "find" (or select) HTML elements based on their id, class, type, attributes, attribute values, and so on. It is based on the existing CSS selector, and in addition, it has some custom selectors of its own.

All selectors in jQuery begin with the dollar sign: $().


Element Selector

The jQuery element selector selects elements based on their element name.

Select all ... in the page

element:

$("p")

Example

After the user clicks the button, all ...

elements are hidden:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

p

"

)

.

hide

(

)

;

}

)

;

}

)

;

Try it »


#id Selector

The jQuery #id selector selects the specified element via the id attribute of an HTML element.

The id of an element on a page should be unique, so to select a unique element on the page you need to use the #id selector.

The syntax for selecting an element by id is as follows:

$("#test")

Example

When the user clicks the button, the element with the id="test" attribute will be hidden:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

#test

"

)

.

hide

(

)

;

}

)

;

}

)

;

Try it »


.class Selector

The jQuery class selector can find elements by a specified class.

The syntax is as follows:

$(".test")

Example

After the user clicks the button, all elements with the class="test" attribute are hidden:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

.test

"

)

.

hide

(

)

;

}

)

;

}

)

;

Try it »


More examples

Element Element
Syntax Description Example
$("*") Select all elements Online example
$(this) Select the current HTML element Online example
$("p.intro") Select the ... with class intro

Element

Online example
$("p:first") Select the first

Element

Online example
$("ul li:first") Select the first
    first element in the
  • Element
Online example
$("ul li:first-child") Select every
    first element in the
  • Element
Online example
$("[href]") Select elements with an href attribute Online example
$("a[target='_blank']") Select all with target attribute value equal to "_blank" Element Online example
$("a[target!='_blank']") Select all with target attribute value not equal to "_blank" Element Online example
$(":button") Select all ... with type="button" elements and Online example
$("tr:even") Select those at even positions
Online example
$("tr:odd") Select those at odd positions
Online example

Using jQuery functions in a separate file

If your website contains many pages and you want your jQuery functions to be easy to maintain, put your jQuery functions in separate .js files.

When we demonstrate jQuery in the tutorials, we add the functions directly to the section. However, it is better to put them in a separate file, like this (referencing the file via the src attribute):

Example

<head>
<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/1.10.2/jquery.min.js" script>
<script src="my_jquery_functions.js" script head>