Xalloumokkelos Xalloumokkelos - 8 months ago 27
Javascript Question

Auto select dropdown based on data-attribute does not run on 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.


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