I understand that this is an old question, but I will lay out a solution for my use case if others encounter the same situation as when implementing James Hill's answer (above) .
I found this question trying to solve the same problem. James's answer gave me 90%. However, for my use case, selecting an item from the drop-down list also caused an action on the page from the onchange drop-down menu. James's code , as written, did not trigger this event (at least in Firefox, which I tested). As a result, I made the following minor changes:
function setSelectedValue(object, value) { for (var i = 0; i < object.options.length; i++) { if (object.options[i].text === value) { object.options[i].selected = true; object.onchange(); return; } } // Throw exception if option `value` not found. var tag = object.nodeName; var str = "Option '" + value + "' not found"; if (object.id != '') { str = str + " in //" + object.nodeName.toLowerCase() + "[@id='" + object.id + "']." } else if (object.name != '') { str = str + " in //" + object.nodeName.toLowerCase() + "[@name='" + object.name + "']." } else { str += "." } throw str; }
Pay attention to the call to object.onchange() , which I added to the original solution . This calls the handler to verify that the action on the page is occurring.
Edit
Added code to exclude if the value parameter is not found; it is necessary for my use.
Doug R. Feb 12 '16 at 16:19 2016-02-12 16:19
source share