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

132
Visualizações
How do I target a previous Element child by <tag> that is further back than just the previous element?

The problem I am having is targeting the text in the input tag in the HTML below with Button that is Copying that text (words). What do i need to change to the JS to get it to target that Input value?

I want to do this without using ID - as I plan on replicating this many many times and don't want to set up individual Id's for the Input tags.

If there is a way to refer to the Class - 'columns-small-8' - child element, then maybe that would work?

Thanks in advance

JS

<script> 

window.onload=function(){
let btns = document.getElementsByTagName("button")
Array.from(btns).forEach(ele => {
  ele.addEventListener("click", function() {
   jimsFunction(this)
  });
})  
function jimsFunction(input) {
  let x = input.previousElementSibling
  let ele = x.previousElementSibling
  ele.select()
  ele.setSelectionRange(0, 99999)
  navigator.clipboard.writeText(ele.value)
  alert("Copied: " + ele.value)
}}

</script>

HTML

       <div>
          <div class="row collapse">
            <div class="columns small-8">
              <input type="text" value="words" readonly>
            </div>
            <div class="columns small-2">
              <a class="button full-width secondary" href="www.google.com" 
              target="_blank">View</a>                                
            </div>
            <div class="columns small-2">
              <button type="button" class="button full-width secondary">Copy URL</button>
            </div>
          </div>
        </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

With your html structure, you can call to parent of it same as :

window.onload=function(){
let btns = document.getElementsByTagName("button")
Array.from(btns).forEach(ele => {
  ele.addEventListener("click", function() {
   jimsFunction(this)
  });
})  
function jimsFunction(input) {
  let x = input.parentElement.parentElement
  let ele = x.firstElementChild.firstElementChild
  ele.select()
  ele.setSelectionRange(0, 99999)
  navigator.clipboard.writeText(ele.value)
  alert("Copied: " + ele.value)
}}
<div>
  <div class="row collapse">
    <div class="columns small-8">
      <input type="text" value="words" readonly>
    </div>
    <div class="columns small-2">
      <a class="button full-width secondary" href="www.google.com" 
      target="_blank">View</a>                                
    </div>
    <div class="columns small-2">
      <button type="button" class="button full-width secondary">Copy URL</button>
    </div>
  </div>
</div>

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