3

Here is my simple code:

$(".test").change(function(){
    alert("user clicked");
});

<select class="test">
    <option value="1">test1</option>
    <option value="2">test2</option>
</select>

It's all simple and working, but I want to get that function called not only when user changes option, but when he clicks on the same option that is already selected, I've tried click event but it gets fired up before user even clicka on any option, what could I do?

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
Linas
  • 4,380
  • 17
  • 69
  • 117

2 Answers2

2

doesn't work in chrome.

$(".test option").click(function(e){
    console.log('click');
});

updated your fiddle.

put the click on the option

EDIT: looks like it isn't possible without a bunch of work like using click on the select and comparing the location it was clicked or something hokey like that.

I am not sure what the end goal is, but the click on the select might work if you can do the processing one extra time when they initially click into it.

another article on it:

Community
  • 1
  • 1
Rob Allen
  • 2,871
  • 2
  • 30
  • 48
1

If you mean that there is only one thing in the select (like in the problem I was having), you could create a default disabled option, and set that as the selected. This would mean that if you had one option, onchange would fire because the disabled was the one that was previously selected:

<option disabled selecte>Select an option</option>
<option> Only Option </option>
Luke Madhanga
  • 6,871
  • 2
  • 43
  • 47