You can find your <select>
tag by its id and save it in a variable using getElementById()
method of a document
object like this:
let selectSize = document.getElementById('size');
Then you can call the selectSize.option
key which will return you an HTMLOptionsCollection
object that has multiple useful properties such as length
. You can then just call for the needed key in that object. For example, if i want to find Dog in your select field i'll do the following:
let selectSize = document.getElementById('size');
let keys = selectSize.options;
for (key of keys) {
if (key.innerHTML === 'Dog') {
console.log('Found a dog at index ', key.index, ' its value is ', key.value);
}
}
Which will print out the following result:
Found a dog at index 1 its value is 72453
More about Document object
More about getElementById()
More about for...of