Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda