I have the following JavaScript methods
// METHOD 1
let categoryType = document.getElementById("create-listing-category-type");
let categoryTypeOption = categoryType.getElementsByTagName("option")[categoryType.selectedIndex].getAttribute("value");
// METHOD 2
let categoryType = document.getElementById("create-listing-category-type");
let categoryTypeOption = categoryType.options[categoryType.selectedIndex].value;
<div class="verification-main-input-div">
<p class="verification-main-text">Category</p>
<div class="drop-dawn-add-game-list">
<div class="arrow-drop-down-games-main">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right chevron-for-drop-down"><polyline points="9 18 15 12 9 6"></polyline></svg>
</div>
<select id="create-listing-category-type" name="cars" class="drop-dawn-add-game-select">
<option value="Coins">Coins</option>
<option value="Items">Items</option>
<option value="Boosting">Boosting</option>
</select>
</div>
</div>
What I am trying to accomplish is to get the data of the selected option from <select>
tag. When I just console.log(categoryTypeOption);
I get the default value, which is Coins
, regardless if I choose another one. What am I doing wrong?
These two methods are based on answers I found when researching a solution.