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

85
Visualizações
How do I get value of an element after clicking another?

In React, how can I get the value of another element by clicking/changing a different one?

<div>
    <input {get this value}/>
    <button onClick={()=> console.log(get value of above input field)}/>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use document.getElementById().value

export default function App() {
  function getText() {
    var myInputText = document.getElementById('myInput').value;
    console.log('Input text:', myInputText);
  }
  return (
    <div>
      <input id="myInput" />
      <button onClick={() => getText()}>Get Text</button>
    </div>
  );
}

Or You can use Forms and useState Hook

export default function App() {
  const [name, setText] = useState('');

  const handleSubmit = (evt) => {
    evt.preventDefault();
    console.log(`Input text: ${name}`);
  };
  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={name}
        onChange={(e) => setText(e.target.value)}
      />
      <input type="submit" value="Submit" />
    </form>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

1) Controlled input using useState hook

Codesandbox Demo

  const [inputValue, setInputValue] = useState("");

  function onInputChange(e) {
    setInputValue(e.target.value);
  }

  const getValue = () => console.log(inputValue);

  return (
    <div>
      <input value={inputValue} onChange={onInputChange} />
      <button onClick={getValue}>get value</button>
    </div>
  );

2) Uncontrollect input using useRef hook

Codesandbox Demo

  const inputRef = useRef("");

  const getValue = () => console.log(inputRef.current.value);

  return (
    <div>
      <input ref={inputRef} />
      <button onClick={getValue}>get value</button>
    </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