0

I have an image on my page that when hovered changes the selectedIndex of a select list.

// change the selected index of the select list on element hover
$("#mySelectList").attr('selectedIndex', 1); // works

The select list also fires another function on change. This works when I manually change the options from the select list but not when I change the selectedIndex on the hover event (code above).

My question, is there a way that I can also send a "click" event or something similar? Basically I want to be able to change the selectedIndex of the select list and also have it fire the other function.

IntricatePixels
  • 1,219
  • 6
  • 29
  • 55

3 Answers3

1

What you can try is to trigger the change (or click or whetever) event.

$('#mySelectList').trigger('change');

or

$('#mySelectList').trigger('click');

after you've set the selected index

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

You can fire the onchange event of the select control:

How do I programmatically force an onchange event on an input?

Just use:

$("#mySelectList").onchange()
Community
  • 1
  • 1
zsalzbank
  • 9,685
  • 1
  • 26
  • 39
0

Why not refactor the function that you would perform normally in the click event into a JavaScript function and then call it directly?

rcravens
  • 8,320
  • 2
  • 33
  • 26
  • hi rcravens - i needed it to call the function from the select list since the select list passes values to the function. i'm sure there is a way to surpass going through the select list though and invoking the function directly from he hover / click event on the original element. Just haven't figured that out. but trigger('change') works for now. – IntricatePixels Dec 19 '10 at 00:08