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

271
Visualizações
Swap inputs on button press

New to JS, need to swap the 2 non-whitespace input values (text) with each other via button. This is what I've tried:

function switcher() {
  var f = document.getElementById('inp').value;
  var s = document.getElementById('inp2').value;
  [f, s] = [s, f];
  if (f === '' || f === ' ', s === '' || s === ' ') {
    alert("fill a blank!");
  } else if (f !== '' || f !== ' ', s !== '' || s !== ' ') {
    f = s;
    s = f;
  }
}
<input type="text" id="inp" value="" size="50">
<input type="button" id="btn" value="Поменять местами" onclick="switcher()">
<input type="text" id="inp2" value="" size="50">

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

0

Here would be my solution:

function switcher() {
  const input1 = document.getElementById('input1');
  const input2 = document.getElementById('input2');
  const text1 = input1.value
  const text2 = input2.value
  if (isStringEmpty(text1) || isStringEmpty(text2)) return alert("fill a blank!")
  input1.value = text2
  input2.value = text1
}

function isStringEmpty(string) {
  return !string.trim().length
}
<input required type="text" id="input1" value="" size="50" />
<input type="button" id="btn" value="Поменять местами" onclick="switcher()" />
<input required type="text" id="input2" value="" size="50" />

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