views:

17

answers:

1

I have a select that has several options. No empty option.

Before calling the trigger none of the options are selected, after it runs the first option is preselected, and I want that the select to stay as it was, no option selected

$("#selProduct").trigger('change');

How to fix this? I still want to trigger the change, to run the events, but I do not want the first option selected.

+1  A: 

I think what you're seeing is the result of the DOM once it's touched and will depend on the browser (but selectedIndex should be 0 already).

That being, said, use .triggerHandler() to run just the handler, like this:

$("#selProduct").triggerHandler('change');

For your question though, you may want to store something on the <select> on page-load, that changes when it's "touched", for example:

$("#selProduct").change(function() { 
  $.data(this, 'changed', false);
  //do stuff
}).data('changed', false);
Nick Craver
I used `if ($("#selProduct").val>0) { $("#selProduct").trigger('change'); }`
Pentium10