Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - How to find an element using class and attribute

I am trying to figure out the most efficient way to find my element. Following i smy structure:

<div class="a" customattrib="2">

in order to find this element can I do something like :

$("div.a [customattrib='2']")

This does not seem to work, is there another way to do this?

Without the class I am able to get the value but I do not think this is efficient enough for my structure:

$("div [customattrib='2']")
like image 845
Murtaza Mandvi Avatar asked Jan 27 '10 16:01

Murtaza Mandvi


People also ask

How do I find an element by its attribute?

The [attribute^="value"] selector is used to select elements with the specified attribute, whose value starts with the specified value. The following example selects all elements with a class attribute value that starts with "top": Note: The value does not have to be a whole word!

How do you check if an element has a specific class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How get data attribute value in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.


1 Answers

Remove the space:

$("div.a[customattrib='2']")

By putting in the space, you're making it into a descendant selector which finds all elements that match [customattrib='2'] and are inside an element that matches div.a.

like image 148
SLaks Avatar answered Oct 02 '22 20:10

SLaks