Wanted to change the sentence "watch yesterday's broadcast" to "watch the webinar now"
3 Answers
What about selecting it by using its DOM order?
var parent = document.querySelector("select");
var element = parent.querySelectorAll("option")[1];
element.innerHTML = "watch the webinar now";
This article can help: How to get the child element of a parent using JavaScript?
Comments
You can use querySelector to get an specific child inside an element and change it.
Snippet
In this case I use
querySelector(':nth-child(2)')to get the second child.
var parent = document.querySelector(".someClassName");
console.log(
parent.querySelector(':nth-child(2)').text
)
document.getElementById('btn').addEventListener('click', () => {
parent.querySelector(':nth-child(2)').text = 'asdasd asdasd';
})
<button id='btn'>change</button>
<select class="someClassName">
<option>aker including versions of Lorem Ipsum</option>
<option>Donec semper tortor ac velit tempus,</option>
<option>vel pellentesque lorem gravida</option>
</select>
1 Comment
mousetail
Even better you can use
option:nth-child() to exclude other elements that might be the second under their parent
document.querySelectorAll('[value]')this will return an object with all the elements that have a value attribute.