I want to trigger a function when i add a new option in a select. This is the code in the select
<select name="projects" id="projects" onChange={handleChange}>
{Object.keys(items.projects).length !== 0 ?
items.projectList.map(i => {return<option>{i}</option>})
: <option>Create new project</option> }
</select>
Function where i trigger the adding of option in select
function handleAdd(event,name,desc){
itemDispatch({type:'ADD_PROJECT', name, desc});
document.getElementById("projects").dispatchEvent(new Event ('change'));
event.preventDefault();
}
The handleChange function
function handleChange(){
alert("change");
}
Read more here: https://stackoverflow.com/questions/66272308/trigger-onchange-when-adding-removing-an-option-on-a-select-reactjs
Content Attribution
This content was originally published by sizzlingseal at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.