Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  93] [ 5]  / answers: 1 / hits: 37267  / 11 Years ago, thu, april 11, 2013, 12:00:00

I am trying to find a way to run a JavaScript function when a user selects an option in an HTML select box, regardless of whether this option was already selected. So onchange is out of the question.



The problem with using onblur is that (in Chrome and Safari, at least) the event is not triggered until the user clicks another element. This can also prove annoying if the user focuses on the select, then clicks away without choosing an option, in which case I do not want the event to be triggered.



I was able to get some success by giving each of the options an onmouseup handler, but this only works in Firefox, as far as I can tell. Any ideas?


More From » select

 Answers
62

Since nobody has bothered to answer this, I'll post a generic version of my code:



<select id=mySelect onfocus=this.selectedIndex=0; onchange=userDidSomething(event)>
<option>Choose one:</option>
<option>Option 1</option>
<option>Option 2</option>
</select>


The JavaScript:



function userDidSomething(event) {
// Your Code Here.
}

[#78978] Wednesday, April 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;