ここで私はいくつかのオプション要素を持っています。それらの値appleを保持しています。私は値appleを保持するoption要素のidを取得したいと思います。 .Butは特定のオプション要素のid値を取得する
<!DOCTYPE html>
<html>
<body>
<form>
Select a fruit:
<br>
<select id="mySelect" size="4">
<option id='for_apple'>Apple</option>
<option>Pear</option>
<option>Banana</option>
<option>Orange</option>
</select>
</form>
<br>
<button onclick="myFunction()">Remove selected fruit</button>
<script>
function myFunction() {
var str="Apple";
var x = document.getElementById("mySelect");
if(x[x.selectedIndex].value == str){
alert((x[x.selectedIndex].value).parentElement.id);
}
}
</script>
</body>
</html>