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

113
Visualizações
toggle input field on an anchor click

I'm trying to make it so when the user clicks "Link" then it scrolls down to the input field "a" and toggles it without having to click. I'm already making use of jQuery so perhaps it should handle scrolling as well, however I'm unsure how to deal with toggling (perhaps focusing on is the right terminology) an input field automatically. Note: I omitted all other code for readability.

<a href="#view" class="button">Link</a>

<div id="view">
<input type="text" id="a" name="a">
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Not sure what you mean by 'toggle', but you can use focus to move the cursor (in)to the input field. Here's a snippet where the input field is out of sight. The handling is done using event delegation.

document.addEventListener(`click`, handle);

function handle(evt) {
  if (evt.target.id === `view`) {
    // if you click from a href element, you have to prevent
    // the default action of the click. May be advisable to
    // use a button here.
    evt.preventDefault();
    const inpVw = document.querySelector(`#a`);
    // focusing automatically scrolls to the field
    inpVw.focus();
    inpVw.setAttribute(`placeholder`, `Feel free to type`);
  }
}
#viewInput {
  margin-top: 120vh;
}
<a id="view" href="#">Link</a>

<p id="viewInput">
  <input type="text" id="a" name="a">
</p>

about 4 years ago · Juan Pablo Isaza Relatório

0

Add an empty anchor tag:

<div id="view">
<input type="text" id="a" name="a">
<a name="myawesomeanchour"></a>
</div>

then add the event handler to the 'Link' button:

var myawesometag = $("a[name='myawesomeanchour']");
$('html,body').animate({scrollTop:myawesometag.offset().top},'slow');
$('#a').toggle();

remove the toggle line if the toggle isn't still required.

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