George Pandurov George Pandurov - 1 year ago 46
jQuery Question

Is there a way in plain javascript to select element in options by value?

As you can see from the title, I am wondering if there is a way in plain javascript to select an element from option fields by some value. I know I can do it with jQuery like this -

option[value="'+ myObject.value+'"]
but I want to know if I can do the same with javascript without using some loop to find the index and then select it by its value.

P.S. I guess jQuery is doing exactly this^ but I am not sure.

Answer Source

You can use querySelector with css attribute equals selector.

var ele = document.querySelector('option[value="' + myObject.value+ '"]');