Is there any method other than running a for loop to check if a value exists in select box using JavaScript
?
I am looking for something like document.getElementById('selbox').valueExists('myval');
with ecma6:
let option = document.getElementById('selbox').querySelector('[value="' + my_value + '"]');
this will find the first element that contains the attribute value="my_value".
PS: sorry by my spanglish :)
You can't extend the methods the select
-element has. So there will not be a solution without an extra function to check for the existence of a value in a select
.
A "solution" without a loop could be the following...
function SelectHasValue(select, value) {
obj = document.getElementById(select);
if (obj !== null) {
return (obj.innerHTML.indexOf('value="' + value + '"') > -1);
} else {
return false;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With