-1

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.

George Pandurov
  • 384
  • 3
  • 15

2 Answers2

3

You can use querySelector with css attribute equals selector.

var ele = document.querySelector('option[value="' + myObject.value+ '"]');
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

You can do it as easily in plain JavaScript by using document.querySelectorAll:

document.querySelectorAll("option[value='" + myObject.value + "']");
Angel Politis
  • 10,955
  • 14
  • 48
  • 66