Xalloumokkelos Xalloumokkelos - 5 months ago 15
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

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

document.addEventListener('DOMContentLoaded', function() {
document.querySelector('option[data-id="'+param1+'"]')
.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

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