Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
¿Cómo obtengo el valor de un elemento después de hacer clic en otro?

En React, ¿cómo puedo obtener el valor de otro elemento haciendo clic/cambiando uno diferente?

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

0

Puede usar 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> ); }

O puede usar Formularios y 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 Report

0

1) Entrada controlada usando gancho useState

Demostración de Codesandbox

 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) Entrada sin control usando el gancho useRef

Demostración de Codesandbox

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!