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

108
Visualizações
Javascipt Trying to get contents of <a> tage

I have been trying to get full detail of a series of tags on an HTML page I can get the ID and text but all other values elude me The HTML looks like this

<a class="cuenta" codigo="1.1.1.1" nombre="Caja" data-placement="right" data-popover-content="#popover_content_wrapper" data-title="Menú Opciones" data-trigger="focus" desc="1.1.1.1 Caja" href="javascript:void(0);" id="619060" padre="619059" rel="" style="color:black;">1.1.1.1 Caja</a>

1.1.1.1 Caja

The code that works

var x = document.querySelectorAll("a");
var myarray = []
for (var i=0; i<x.length; i++){
var allcontent = x[i].elements
var nametext = x[i].textContent;
var cleantext = nametext.replace(/\s+/g, ' ').trim();
var classtype = x[i].class;
var nombretxt = x[i].nombre;
var idno = x[i].id;
var padreno = x[i].padre;
myarray.push([idno,nametext]);
};
function make_table() {
    var table = '<table><thead><th>idno</th><th>nametext</th></thead><tbody>';
   for (var i=0; i<myarray.length; i++) {
            table += '<tr><td>'+ myarray[i][0] + '</td><td>'+myarray[i][1]+'</td></tr>';
    };
 
    var w = window.open("");
w.document.write(table); 
}
make_table()

I need to get the values of class= codigo= padre= nombre= into the array as well but experimenting with var nombretxt = x[i].nombre; and similar does not return a value

Many thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • Using DOM's getAttribute() property

const aTag = document.querySelector("a");
let padreno = aTag.getAttribute("padre");
let classtype = aTag.getAttribute("class");
let nombre = aTag.getAttribute("nombre");
console.log({ padreno, classtype, nombre });
//{padreno: '619059', classtype: 'cuenta', nombre: 'Caja'}
<a
    class="cuenta"
    codigo="1.1.1.1"
    nombre="Caja"
    data-placement="right"
    data-popover-content="#popover_content_wrapper"
    data-title="Menú Opciones"
    data-trigger="focus"
    desc="1.1.1.1 Caja"
    href="javascript:void(0);"
    id="619060"
    padre="619059"
    rel=""
    style="color: black"
    >1.1.1.1 Caja</a
>

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