Home > database >  how to get a data-attribute where has class active
how to get a data-attribute where has class active

Time:06-23

i have a multiple anchor tag like below

<a  data-type="desc" data-field="relevance" href="#" title="" target="_self">relevance</a>
<a  data-type="desc" data-field="most-viewed" href="#" title="" target="_self">most viewer</a>
<a  data-type="desc" data-field="high-price" href="#" title="" target="_self">high-price</a>

my question is how can i get the data-field where has class active in it

CodePudding user response:

A pure JS solution will be:

// document.querySelector(".sort-type.active") - Gets the element with active class
// getAttribute('data-field') - Gets the attribute required from the html

document.querySelector(".sort-type.active").getAttribute('data-field')
  • Related