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

80
Views
Reaccionar agregando accesorios personalizados a elementos html de vainilla
 const handleChange = (e) => { console.log(e.target.id); }; return ( <div> <select onChange={(e) => handleChange(e)}> <option value="1-10" id="foo"> 1-10 </option>

¿Cómo puedo hacer que el id prop en la etiqueta <option> sea accesible mediante el código anterior? e.target.id no devuelve nada, pero e.target.value devuelve el valor seleccionado. ¿Cómo puedo crear estos atributos personalizados cuando uso elementos html de vainilla?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Uno de los métodos más fáciles para lograr esto es el siguiente:

 const handleChange = (e) => { const index = e.target.selectedIndex; const id = e.target.childNodes[index].id; console.log(id); // logs 'foo' or 'bar' depending on selection }; return ( <div> <select onChange={(e) => handleChange(e)}> <option value="1-10" id="foo"> 1-10 </option> <option value="11-20" id="bar"> 11-20 </option> </select> </div> );
about 4 years ago · Juan Pablo Isaza Report

0

e.target es la select , no la option . Y dado que la select no tiene una id , no obtendrá nada. Una forma de lograr lo que quieres es hacerlo:

 export default function App() { const handleChange = (e) => { const selectedOption = e.target.querySelector(`option[value='${e.target.value}']`); console.log(selectedOption.id); }; return ( <select onChange={(e) => handleChange(e)}> <option value="1-10" id="foo"> 1-10 </option> <option value="1-11" id="bar"> 1-11 </option> </select> ); }
about 4 years ago · Juan Pablo Isaza Report

0

Aquí hay otra manera de hacerlo

 import React from 'react'; const handleChange = (e) => { const index = e.target.selectedIndex; const el = e.target.childNodes[index] const option = el.getAttribute('id'); console.log(option) }; export function App(props) { return ( <div className='App'> <select onChange={(e) => handleChange(e)}> <option value="1-10" id="foo"> 1-10 </option> <option value="2-10" id="zoo"> 2-10 </option> </select> </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!