You can do it like this:
- add the
data-select attribute to the button, which will contain the value item to select; - hang event handler on the block with buttons;
- in the handler, we look at whether the element that called it has the
data-select attribute, if there is, then select the item in the drop-down list; - call the
change event.
The complete jsFiddle example:
document.getElementsByClassName('btn-group')[0].addEventListener('click', function(evt) { var elem = evt.target; if (elem.hasAttribute('data-select')) { document.getElementById('select').value = elem.getAttribute('data-select'); var elem = document.getElementById('select'); var event = new Event('change'); elem.dispatchEvent(event); } }, false);
When setting "manually", after changing the value, read the .value fields in select , the change event occurs. Therefore, after programmatically selecting the desired item in the list, you need to trigger this event. I described the solution in pure JS, all this can be easily rewritten using jQuery, then the event will be called like this:
$(select).trigger('change');
val1in the list, as if it were selected manually? - PashaPash ♦