Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selectors on custom data attributes using HTML5

I would like to know what selectors are available for these data attributes that come with HTML5.

Taking this piece of HTML as an example:

<ul data-group="Companies">   <li data-company="Microsoft"></li>   <li data-company="Google"></li>   <li data-company ="Facebook"></li> </ul> 

Are there selectors to get:

  • All elements with data-company="Microsoft" below "Companies"
  • All elements with data-company!="Microsoft" below "Companies"
  • In other cases is it possible to use other selectors like "contains, less than, greater than, etc...".
like image 238
Jose3d Avatar asked Nov 10 '10 16:11

Jose3d


People also ask

How can custom attributes be defined in html5 for a?

If you want to define your own custom attributes in HTML, you can implement them through data-* format. * can be replaced by any of your names to specify specific data to an element and target it in CSS, JavaScript, or jQuery.

How do I select HTML element based on data attribute?

Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document. Here is the HTML for the examples in this article.

Which jQuery selector is used to retrieve all elements with the attribute data attribute name?

You can use the attribute selector: $('[data-my-key]').

How set data attribute in jQuery?

To set an attribute and value by using a function using this below syntax. $(selector). attr(attribute,function(index,currentvalue)) ; To set multiple attributes and values using this below syntax.


1 Answers

$("ul[data-group='Companies'] li[data-company='Microsoft']") //Get all elements with data-company="Microsoft" below "Companies"  $("ul[data-group='Companies'] li:not([data-company='Microsoft'])") //get all elements with data-company!="Microsoft" below "Companies" 

Look in to jQuery Selectors :contains is a selector

here is info on the :contains selector

like image 150
John Hartsock Avatar answered Oct 08 '22 17:10

John Hartsock