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']")
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!
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".
Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With