I'm working on a project with a chrome plugin to manipulate a web site content and add some more functionalities.
I have a <select>
element I want to manipulate:
<select id="thePage:form:pBlock:address:addressTypeSelectionDropdown" name="thePage:form:pBlock:address:addressTypeSelectionDropdown" size="1" onchange="onchangeOfPicklist();">
<option value="User Entry">User Entry</option>
<option value="CustomerID">CustomerID</option>
<option value="Closest to Customer">Closest to Customer</option>
<option value="Location">Location</option>
<option value="Customer's Address" selected="selected">Customer's Address</option>
</select>
I would like to change the select option in specific if conditions, but this doesn't seems to work whatever I do. So far I have tried almost everything, but it seems the onchange trigger is doing the magic, otherwise click simulations and trying to change options doesn't seem to work. So far I have tried different approaches, but they all seems not to trigger an option:
let customerAddress = labelAddress.parent().next().first().find("select");
customerAddress.children('[value="Closest to Customer"]').trigger("change");
and also :
customerAddress.children('[value="Closest to Customer"]').attr("selected", "selected");
customerAddress.children('[value="Closest to Customer"]').change();
and
customerAddress
.find('option:Closest to Customer')
.prop('selected',true)
.trigger('change');
and even simulating keypress:
let e = jQuery.Event("keydown");
e.which = 13; // # enter
customerAddress.focus();
customerAddress.trigger(e);
Here I have no idea why the event is not being triggered.
I'm adding an event listener and the console doesn't indicate the event trigger:
document.addEventListener("keydown", function (event) {
console.log("KeyPress: " + event.which);
});
or like this :
let e = jQuery.Event("keydown");
e.which = 13; // # enter
customerAddress.focus();
customerAddress.attr("size", 5);
customerAddress.children('[value="Closest to Customer"]').focus();
customerAddress.children('[value="Closest to Customer"]').trigger(e);
Please note that I'm working on the chrome plugin and I only can change the source code of this plugin and manipulate elements on the webpage, not able to change the source code of the webpage / scripts and functions.
My question is, is there a way to trigger the embedded onchange function in the <select>
element?