-1

I've been searching for already a long time but I can't find anything so I have a question.

I have a form select option field and that form has 5 select fields. When you select something in the first field the second field will fill itself with option with what you've selected. I think that the update is getting done by Ajax. But I also want to use the form option field with buttons(div/a) that will select an option in the select fields but when you use

$('#finder-2--5').val("413058").attr('selected', 'selected').parent().focus();

the field is not clicked so the next field won't reload.

So is there a possible option to fake a real mouseclick in javascript/jQuery.

Thanks in advance.

  • `But I also want to use the form option field with buttons(div/a) that will select an option in the select fields`... Are you saying you want to be able to click a `button` or `anchor` which then will select one of the options in the ` – Nope Apr 03 '13 at 22:04
  • 1
    bind `change` handler to ` – charlietfl Apr 03 '13 at 22:11

1 Answers1

1

I guess you want to dispatch a click event from the option element. Since you're using jQuery, you can use the trigger method. Some browsers in use will not respond to clicks on option elements, though I think jQuery's event model provides a substitute.

Setting the selected attribute doesn't necessarily make an option selected, it just sets the selected attribute (which is a boolean attribute) and affects the defaultSelected property. To make an option selected, either set the select's selectedIndex property to the index of the option, or set the option's selected property to true (i.e. boolean true, not the string "true") using the prop method.

Edit

It seems that the best option is to put a change listener on the select element.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • In addition to add a solution to OPs issue of auto-filling the second select based on the selection of the first. OP should extrapolate the code which loads the values into the second select into a separate function which then can be called from either the `click/change` event of the first select or manually after selecting an option programmatically using one of the suggested methods. That would possibly be cleaner than simulating a click event after the option has been selected. – Nope Apr 03 '13 at 22:26