Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Javascript - Get an element regarding its data-slug

I have on a webpage several buttons like that :

<div class="jet-portfolio__filter-item" data-slug="slug-one">
<div class="jet-portfolio__filter-item" data-slug="slug-two">

I'm catching a slug parameter passed in url that tells me which button I should click on page load. Question is how can I get the right button using javascript, since they don't have ids, to click it ?

Thanks !

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can do this very simple with the onclick JS function. Then you have two options for reading out the data element. 1) getAttribute() and 2) dataset

function myFn(elem) {
  console.log('with getAttribute:', elem.getAttribute('data-slug'))
  console.log('with dataset:', elem.dataset.slug)  
}
<div class="jet-portfolio__filter-item" data-slug="slug-one" onclick="myFn(this)">click me! Slug One</div>      
<div class="jet-portfolio__filter-item" data-slug="slug-two" onclick="myFn(this)">click me! Slug Two</div>

about 4 years ago · Juan Pablo Isaza Report

0

You've to use attribute selector to select those elements (assuming you're not using their classes to select them).

Here's the CSS attribute selector syntax: tag[attribute=value]. I think you can also replace tag with class or id but I'm not sure.

Now you can use this syntax in document.querySelector(selector).

const slugOne = document.querySelector("div[data-slug=slug-one]");

console.log(slugOne.innerText);
<div data-slug="slug-one">Content Of Slug One</div>
<div data-slug="slug-two">Content Of Slug Two</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can select all elements by class name with "getElementByClassName" and then looping all elements and do addEventListenet to all selected items and pass a function to get the attribute of each element when the user does click.

var elements = document.getElementsByClassName("jet-portfolio__filter-item");

var onClickItems = function() {
    var slug = this.getAttribute("data-slug");
    console.log(slug)
};

for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', onClickItems);
}
<div class="jet-portfolio__filter-item" data-slug="slug-one">Container number 1</div>
<div class="jet-portfolio__filter-item" data-slug="slug-two">Container number 2</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!