0

I've been trying to log my event object value in 'option' tag and it does not work, but it does work in select tag, here is my code:

{data.map((item, index) => { const {name, options} = item; return ( <div className="item" key={name}> <label htmlFor={name}> Select any {name}: <select name={name} id={name} onClick={(e) => console.log(e.target.value)}> <option disabled selected>Any {name}</option> {options.map((item, index) => { return ( <option value={item} key={index} onClick={(e) => console.log(e.target.value)} >{item}</option> ) })} </select> </label> </div> ) })} 

what's wrong with my option tag?

5
  • Are you expecting the event to fire each time you change the value? Or when you click the select-input and the pop-up opens? Commented Mar 5, 2021 at 9:01
  • Same question as @Phoenix1355 asked. Do you want the console to log on change or on user click? Commented Mar 5, 2021 at 9:05
  • yes I want the event to fire each time I click on each option Commented Mar 5, 2021 at 9:07
  • 1
    Maybe you want to take a look at stackoverflow.com/questions/3487263/… or this (it says that option does not trigger onClick in chrome) stackoverflow.com/questions/22482842/… Commented Mar 5, 2021 at 9:34
  • 1
    please refer to this question forum.freecodecamp.org/t/… Commented Mar 5, 2021 at 10:08

1 Answer 1

1

I don't think there is anything wrong with your option tag. Did you encounter this issue in Chrome?

This seems to be a known issue in Chrome: The onclick event does not work for options

Indeed, I tried onClick with both Chrome and Firefox - for the option tag, onClick does fire in Firefox but not in Chrome.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.