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
}
}
document.querySelectorAll('select option[value="something"]').length > 0?foldersList.lengthshould befoldersList.options.length.