<p>
Selected option text: <span class="text-output">none</span>
<p>
<p>
Selected option value: <span class="value-output">none</span>
<p>
<select class="pizza-select">
<option value="four_meats">Four Meats</option>
<option value="royal_cheese">Royal Cheese</option>
<option value="vegetarian">Vegetarian</option>
<option value="smoked_salmon">Smoked Salmon</option>
</select>
body {
margin: 16px;
}
select {
padding: 4px;
}
const select = document.querySelector(".pizza-select");
const textOutput = document.querySelector(".text-output");
const valueOutput = document.querySelector(".value-output");
select.addEventListener("change", setOutput);
function setOutput(event) {
const selectedOptionValue = event.currentTarget.value;
const selectedOptionIndex = event.currentTarget.selectedIndex;
const selectedOptionText =
event.currentTarget.options[selectedOptionIndex].text;
textOutput.textContent = selectedOptionText;
valueOutput.textContent = selectedOptionValue;
}
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.