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

205
Visualizações
¿Cómo cambio el valor de <label for> usando JavaScript?

Tengo un cuadro de texto HTML simple con una etiqueta y quiero poder cambiar el valor de la etiqueta usando JavaScript. He intentado varios métodos como se describe a continuación, pero nada parece tener éxito. ¡Cualquier ayuda sería apreciada!

HTML:

 <html> <body> <li id="job_number_wrapper"> <label for="job_number">I WANT TO CHANGE THIS TEXT</label> <input type="text" id="job_number" name="job_number"> </li>

JavaScript:

 <script> function myFunction() { document.getElementById("job_number").htmlForVal = "New text"; //does nothing if (job_number_wrapper.textContent) job_number_wrapper.textContent = "New Text"; //changes text, but removes text box if (job_number.textContent) job_number.textContent = "New Text"; //does nothing $("label[for=job_number]").html("New Text"); //does nothing $("label[for=job_number]").text("New Text"); //does nothing document.getElementById('job_number').innerHTML = 'New Text'; //does nothing document.getElementById('job_number').innerText = 'New Text'; //does nothing document.getElementById('job_number').value = 'New Text'; //does nothing $('#job_number').val("New Text"); //populates the textbox value, but does not change the label var label = document.querySelector('label[for="job_number"]'); //does nothing label.textContent = "New Text" //does nothing } myFunction(); </script>
 </body> </html>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Te estás perdiendo el ' dentro.

 $("label[for='job_number]'").html("New Text"); $("label[for='job_number]'").text("New Text");
about 4 years ago · Juan Pablo Isaza Relatório

0

No está seleccionando la etiqueta con document.getElementById("job_number") . En su lugar, intente esto:

 const labelEl = document.querySelector('#job_number_wrapper label');

O bien, puede asignar una identificación o clase a su etiqueta y seleccionarla directamente.

about 4 years ago · Juan Pablo Isaza Relatório

0

Casi acertaste cuando intentaste la llamada a querySelector :

 var label = document.querySelector('label[for="job_number"]'); //does nothing label.textContent = "New Text" //does nothing

La única razón por la que este selector falla son las comillas dobles que está utilizando para seleccionar el job_number . Quítalos y funcionará:

 var label = document.querySelector("label[for=job_number]"); label.textContent = "New Text";
 <label for="job_number">I WANT TO CHANGE THIS TEXT</label> <input type="text" id="job_number" name="job_number" />

Ver también: pregunta relacionada

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