Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery, selector for class within id

Tags:

jquery

Below, how should I select the elements that contain the class my_class within the element with id = "my_id"?

Note that the element may also have another class, which I am not selecting for.

<div id = "my_id">     <span class = "my_class hidden">hi</span>     <span class = "my_class">hello</span> </div> 

was trying

$("#my_id [class*=my_class ]") 
like image 453
user984003 Avatar asked Jul 02 '13 04:07

user984003


People also ask

How do I select an item using class and id?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How do you select element by id in jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

What is difference between the id selector and class selector in jQuery?

Difference between id and class attribute: The only difference between them is that “id” is unique in a page and can only apply to at most one element, while “class” selector can apply to multiple elements.

What does addClass do in jQuery?

jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.


2 Answers

You can use the class selector along with descendant selector

$("#my_id .my_class") 
like image 123
Arun P Johny Avatar answered Oct 04 '22 08:10

Arun P Johny


Just use the plain ol' class selector.

$('#my_id .my_class') 

It doesn't matter if the element also has other classes. It has the .my_class class, and it's somewhere inside #my_id, so it will match that selector.

Regarding performance

According to the jQuery selector performance documentation, it's faster to use the two selectors separately, like this:

$('#my_id').find('.my_class') 

Here's the relevant part of the documentation:

ID-Based Selectors

// Fast: $( "#container div.robotarm" );  // Super-fast: $( "#container" ).find( "div.robotarm" ); 

The .find() approach is faster because the first selection is handled without going through the Sizzle selector engine – ID-only selections are handled using document.getElementById(), which is extremely fast because it is native to the browser.

Selecting by ID or by class alone (among other things) invokes browser-supplied functions like document.getElementById() which are quite rapid, whereas using a descendent selector invokes the Sizzle engine as mentioned which, although fast, is slower than the suggested alternative.

like image 45
doppelgreener Avatar answered Oct 04 '22 10:10

doppelgreener