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

216
Visualizações
Find Parent Sibling First Child's First Child using Jquery

I'm trying to Use a combination of JS and Jquery here to select the input text (a few elements previous), when the button is clicked.

I know how to do this just using JS but i want to understand how to do this using jQuery. I get the error message in console: TypeError: ele.setSelectionRange is not a function. Which I take it means that it is not defining the Input Value the way I need it to.

I'm not using ID or Class here to identify the input.

Can someone help me here? Thanks

JS

$(document).ready(function () {
  $('.jimmy').click(function(e){
  e.preventDefault;
  jimsFunction(this);
  });
});

function jimsFunction(input) {
  let ele = $(input).parent().siblings(':first-child').children();
  ele.select();
  ele.setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele.value);
  alert("Copied: " + ele.value);
}

HTML

      <div class="colbody">
        <div>
          <input type="text" value="www.brave.com" readonly>
        </div>
        <div>
          <a href="www.google.com" target="_blank">View</a>                                
        </div>
        <div>
          <button type="button" class="jimmy">Copy URL</button>
        </div>
      </div>  
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

setSelectionRange(0, 99999) is not a method on jQuery object. Use it on DOM element.

So try: [0]

Example:

$('.jimmy').click(function(e) {
  e.preventDefault;
  jimsFunction(this);
});


function jimsFunction(input) {
  let ele = $(input).parent().siblings(':first-child').children();
  ele[0].select();
  ele[0].setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele[0].value);
  alert("Copied: " + ele[0].value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="colbody">
  <div>
    <input type="text" value="www.brave.com" readonly>
  </div>
  <div>
    <a href="www.google.com" target="_blank">View</a>
  </div>
  <div>
    <button type="button" class="jimmy">Copy URL</button>
  </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