0

My question is same as this - How can I check whether a option already exist in select by JQuery

Although I want to know how it can be done using JavaScript rather than using jQuery. I've tried using the indexOf() , === among other things, but I'm not sure if my approach is correct.

P.S : If you're marking it as a duplicate, do link me with the question that has the answer in JavaScript. Thank you.

Also, reasons for down voting are welcome.

My approach :

var foldersList = document.getElementById("folders");
    for(var i=0; i<foldersList.length; i++)
    {
       if(!(foldersList.options[i].value.indexOf("someValue") === -1))
       {
             //do something
       }
    }
10
  • Can you show your approach, and tell why you think it is not correct? document.querySelectorAll('select option[value="something"]').length > 0 ? Commented Jun 11, 2014 at 8:25
  • Can you show how you tried to do it and tell us what exactly didn't work? Commented Jun 11, 2014 at 8:26
  • I iterated through the select box to search for the value, is that a correct approach? Commented Jun 11, 2014 at 8:29
  • 1
    It seems that foldersList.length should be foldersList.options.length. Commented Jun 11, 2014 at 8:39
  • 1
    @ivarni—Select elements do have an options collection. @jack—Select elements do have a length property that is the number of options (i.e. the length of the select's options collection). Commented Jun 11, 2014 at 8:46

3 Answers 3

3

You could use document.querySelectorAll:

document.querySelectorAll('#yourSelect option[value="yourValue"]').length > 0

Thats the same as:

$("#yourSelect option[value='yourValue']").length > 0;
Sign up to request clarification or add additional context in comments.

3 Comments

Descendant selectors doesn't work in IE8 if memory serves me right. Though, really, it's about time we stopped targetting IE8.
@ivarni—it's not so much about "targeting" IE8, but supporting users who use it, which is about 1/4 of IE users (depending on whose statistics you believe, of course). :-)
While "target" probably wasn't the best choice of words, I still think we need to move on from IE8 soon. Yes, people are using it, but us supporting that does not help them migrate from it.
2
var yourSelect = document.getElementById("yourSelect");
for (i = 0; i < yourSelect.length; ++i) {
   if (yourSelect.options[i].value == "someValue") {
      alert("someValue is available");
   }
}

Comments

2

The simple way, supported even by the most antique browsers:

function selectHasOption(select, value)
{
    for (var i = 0, len = select.options.length; i != len; ++i) {
        if (select.options[i].value == value) {
            return true;
        }
    }
    return false;
}

And this is the expression:

selectHasOption(document.getElementById('folders'), 'someValue')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.