Here’s my JSFiddle (https://jsfiddle.net/gnf93Lw4/4/) and my code doesn’t seem to be selecting the value and the text as seen in the console when I click the button. Please let me know what wrong I’m doing.
I was selecting it by id so value part is working when I selected it via name but the text part is still not working:
Hi @Jack_Tauson_Sr , there is not element with name='list option:selected'
… it should be
let name = $("[name='list'] option:selected").text()
Hey! You must have missed the actual answer above your post.
So, it looks like you’ve got the value part working (sorry I overlooked your comment), which is awesome! Now, for the text issue—it’s probably just a small tweak that’s needed.
Here’s what you can do: when you’re grabbing the selected option, make sure you’re pulling both the value and the text properly. Your code should look something like this:
function showList() {
let dropdown = document.getElementById("list");
let selectedOption = dropdown.options[dropdown.selectedIndex];
let value = selectedOption.value; // This gets the value
let text = selectedOption.text; // This gets the text
console.log("Value: " + value);
console.log("Text: " + text);
}
With this setup, you should be able to get both the value and the text from the selected option without any issues. Just give it a try, and you should see both showing up in your console when you click the button.
Let me know if that does the trick!
I corrected my post. Thank you for letting me know.