Xalloumokkelos Xalloumokkelos - 1 year ago 130
jQuery Question

Setting dropdown value not working in Safari

I run this jQuery code to autoselect from a dropdown based on the

attribute. The problem is that it does not run on Safari.

document.addEventListener('DOMContentLoaded', function() {
.setAttribute('selected', 'selected');

Any help on how to edit the code to run on Safari?

Please find this JSFiddle https://jsfiddle.net/6uh57nz3/1/ even if it is not working on this, you have the complete code.

Answer Source

Don't know why, but setAttribute is not working in Safari. But, setting the property value of the DOM element works. And this works in all browsers.

document.querySelector('option[data-id="' + param1 + '"]')
    .selected = 'selected';

Updated Fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download