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

172
Views
get data-value with variable value

I have a data-value in my php code that is used in several places, for example:

<a class="dropdown-item" data-value="<?= $i ?>"></a>
<a class="dropdown-item" data-value="<?= $k ?>"></a>

And now I need to use one of these values ​​in the script

getAttribute('data-value')

But I can’t just use data-value, since it will be relevant to everything that I have, I only need to use this data-value="<?= $k ?>"

How can I specify a value with this variable in the script?

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

0

since you want to get the data-value of a specific element with the $k variable, your best bet is giving an id to that specific element and selecting it by id:

HTML

<a id="element_with_k" class="dropdown-item" data-value="<?= $k ?>"></a>

JS

const elementWithK = document.getElementById('element_with_k');

elementWithK.dataset.value // "the content of $k"

In case thete are multiple elements that you want to select then you add an extra class to those elements, query all the elements that have the extra class , loop through them and get each element's value.

Hope this helps, cheers!

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!