I am trying to trigger events when options in a dropdown are clicked on. I couldn't find anything that explains how to do this. What I want is to fire the event and use the value of it. When i use .on instead of .change I fire the event everytime the select is clicked and thats not what I want because I only want to use the value of the selected item.
Assume this is dynamically generated:
<select id="dropdownOne">
<option value="1">NumberOne</option>
</select>
Assume this is dynamically generated:
<select id="dropdownTwo">
<option value="1">NumberOne</option>
<option value="2">NumberTwo</option>
</select>
JS:
$("select").change("click", function () {
//This event is not fired in dropdown one
//This event is only fired in dropdown two if I change the option
var value = $(this).val();
if(value == 1){
console.log("This logs number 1");
}else{
console.log("This logs number 2");
}
}
How can I:
- Trigger the event on an item in a dropdownlist that holds only one item
- Trigger the event on an item that is clicked multiple times
EDIT: So, my question was marked as a duplicate:
How to call onchange event when dropdown values are same
The "duplicate" has 6 answers:
Vicky Kumar: Says the code of that question is fine and working. This answer does not contribute to my problem
ajpocus: Has an answer, got edited later saying that it doesn't work. Doesn't work
Krupesh Kotecha: Uses a click event on the select, same behaviour as .on This is not what I want
Teja: Fixes the problem for that question but not mine, still doesn't trigger any event when te select only has 1 option or when you click an option more than once This is not what I want
charu joshi: Uses .on event with :option selecter, doesn't work because the :option selecter is unknown, can also not be found in the jQuery API documentation Doesn't work
derp: This answer explains how to remove duplicates and then uses the change event, this might work for the question of the "duplicate" but I never load in duplicate items in my select, and even then it still uses the change event which doesn't do what I want as explained in my question This answer does not contribute to my problem
So all of the above answers from the "duplicate" question do not apply to my problem/question. Also none of those answers were marked as accepted by the original poster.
Aucun commentaire:
Enregistrer un commentaire