Wednesday, 24 October 2018

javascript - 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:











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...".


Answer



$("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


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...