Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda