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

134
Views
How to get data output from HTML in JS?

How can I get the '123' value from the following code? I thought the snippet should work but I instead get email :....

<div id="home">
    <div class="name" my-data="123">Email: test@gmail.com</div> 
</div>

var element = document.querySelector('.element');
var dataAttribute = element.getAttribute('my-data');

console.log(dataAttribute);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You try to use querySelector with .element, that class doens't exist instead you need to use .name

var element = document.querySelector('.name');
var dataAttribute = element.getAttribute('my-data');

console.log(dataAttribute);
<div id="home">
    <div class="name" my-data="123">Email: test@gmail.com</div> 
</div>


Just for your knowledge you could use data-attribute like this:

var element = document.querySelector('.name');
var dataAttribute = element.getAttribute('data-email');

console.log(dataAttribute);
<div id="home">
    <div class="name" data-email="123">Email: test@gmail.com</div> 
</div>

Reference:

  • Using data attributes
  • QuerySelector

For pick text of div email use textContent like:

var element = document.querySelector('.name');
var dataAttribute = element.getAttribute('my-data');

console.log(dataAttribute, element.textContent);
<div id="home">
    <div class="name" my-data="123">Email: test@gmail.com</div> 
</div>

Reference:

  • Node.textContent
about 4 years ago · Juan Pablo Isaza Report

0

When using a custom attribute, make sure to write "data-", and then the rest of the attribute name. Doing that, you can easily access the data from javascript.

<div class="name" data-something="123">Email: test@gmail.com</div> 

And for the js:

let element = document.querySelector('.name')
let dataAttribute = element.dataset.something
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!