-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.

0

2 Answers 2

3

You can use querySelector with css attribute equals selector.

var ele = document.querySelector('option[value="' + myObject.value+ '"]');
Sign up to request clarification or add additional context in comments.

1 Comment

I tried to do it with querySelector but I guess I missed something. Thanks for the fast reply.
2

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

document.querySelectorAll("option[value='" + myObject.value + "']");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.