Hi, with a userscript on this uploaded webpage (saved from the originale) I would like to set
“O2 - SCARTO ORDINARIO 2024” as default option value of the combobox of the popup. This is the html of the combobox:
The popup opens after clicking on the button “Aggiungi a lista”, that has this HTML:
<ol data-parent="15030030000000">
<li data-id="15030030100000">
<a title="Aggiungi a lista" data-fn="D_INV_AGGWLIST_DET" href="/sebina/catalogazione/D_INV_AGGWLIST_DET.do?codMenu=15030030100000&codFnz=D_INV_AGGWLIST_DET">
<span class="tooltip"> Aggiungi a lista</span>
</a>
</li>
</ol>
This is what I tried without succes, thank you!
// Set the default value
document.getElementById('comboWorklist').value = '231'; // Assuming '231' is the value for "O2 - SCARTO ORDINARIO 2024"
// Trigger the chosen:updated event
var comboWorklist = document.getElementById('comboWorklist');
var event = new Event('chosen:updated');
// Dispatch the event
comboWorklist.dispatchEvent(event);
Thanks, @rpg_digital & @Thallius but it doesn’t work, the problem is the Chosen Library. This jQuery script works instead, but I want a pure JavaScript solution without using jQuery and without relying on @run-at.
I did already comment on this in a previous thread. chosen is dependent on JQuery or alternatively the Prototype framework.
To get chosen to work with Vanilla JS would involve editing the chosen script — not a good idea.
edit: I also seem to remember from similar issues you had some time ago, that a possible workaround was to simulate clicking on that option. I maybe wrong.
Just a quick search back
In that example I dispatch the change event as well.
I am looking at your uploaded html stato2.html, and on inspection the select form does not look like the code you have posted. I’m guessing chosen does a bit of a re-work to it?
Triggering the chosen:updated event after changing the original select should work. You can test this on the demo page for chosen. Running these lines in the console will successfully change the value of the country select:
Your code that sets the value and triggers the updated event should work. If it’s not, then what you probably have is a timing problem as mentioned. For example, is your extension waiting for the popup to open before running the code? If not, you might be running it too soon.