0

I'm currently trying to both programmatically get, and programmatically set the innerHTML of the selected value from a select-element in plain Javascript. This is my code:

const select = document.querySelector("#myDiv select"); 
console.log(select.selectedOptions[0].option.innerHTML);

If I write console.log(select.selectedOptions), I get the following:

enter image description here

It's the innerHTML inside option that I want to reach and edit, but I get the error

TypeError: Cannot read property 'option' of undefined

I did not find much about this online, hence me asking here. What is the best way to achieve this?

Thanks!

asked Jun 12, 2020 at 9:06
2
  • may I see the detail inside 0: option? Commented Jun 12, 2020 at 9:09
  • selectedOptions contains option elements. And it doesn't have options property. Use selectedOptions[0].innerText Commented Jun 12, 2020 at 9:13

5 Answers 5

1

select.selectedOptions[0] returns the actual option element itself, so you can get and set innerHTML from that directly.

Also make sure you set a selected on an option so that something is selected by default and select.selectedOptions is never empty.

const select = document.querySelector("#myDiv select"); 
console.log("Initial:", select.selectedOptions[0].innerHTML);
const button = document.getElementById('toggle'),
 newText = document.getElementById('newText');
 
button.addEventListener('click', () => select.selectedOptions[0].innerHTML = newText.value);
<div id="myDiv">
 <select id="mySelect">
 <option value="option_a" selected>Option A</option>
 <option value="option_b">Option B</option>
 </select><br>
 <input id="newText" type="text" placeholder="Enter text here..."></input>
 <button id="toggle">Change</button>
</div>

answered Jun 12, 2020 at 9:13
Sign up to request clarification or add additional context in comments.

Comments

0

You mean this?

You do not need the .option after selectedOptions[0]

const select = document.querySelector("#myDiv select"); 
console.log(select.selectedOptions[0].textContent); // or .text
// alternative
console.log(select.options[select.selectedIndex].textContent);
// setting the text:
select.selectedOptions[0].text = "Number one"
<div id="myDiv">
<select>
<option value="1">one</option>
</select>
</div>

answered Jun 12, 2020 at 9:10

Comments

0

Just remove the option before innerHTML. Follow the code.

const select = document.querySelector("#myDiv select"); 
const optionSelected = select.selectedOptions[0]; 
console.log(optionSelected); 
optionSelected.innerHTML = 'New Demo'

strong text

Working Demo

answered Jun 12, 2020 at 9:14

3 Comments

I get TypeError: Cannot read property 'innerHTML' of undefined :/
Not very clear if you changed anything when you set the first value to the same it already is. Anyway I already answered a while ago
@JensOlsson When do you want to change the option label?
0

You can find the selected option using a css selector and read its textContent

console.log(document.querySelector("#myDiv select option:checked").textContent);
<div id="myDiv">
 <select>
 <option value="1">one</option>
 </select>
</div>

answered Jun 12, 2020 at 9:37

2 Comments

I get TypeError: Cannot read property 'textContent' of null... don't really understand why
"don't really understand why" ... That makes two of us. Tested the snippet in Chrome, Firefox and Edge, no problem there.
0

There is a one-liner using the :checked pseudo-class in JavaScript to get either the selected text or value. Here we are assuming we have a select with id="searchList".

const selected = document.querySelector('#searchList :checked');//returns selected node
console.log(selected.innerText);//get the selected option text
console.log(selected.value);//get the selected option value

Verifying that this indeed works for selected options in select elements, here's the official documentation:

The :checked CSS pseudo-class selector represents any radio (), checkbox (), or option ( in a ) element that is checked or toggled to an on state.

:checked MDN

answered Feb 3, 2022 at 20:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.