0

Wanted to change the sentence "watch yesterday's broadcast" to "watch the webinar now"

Wanted to change the sentence "watch yesterday's broadcast" to "watch the webinar now"

5
  • 2
    Why can't it have an id or class? Commented Sep 15, 2021 at 8:14
  • 1
    Familiarize yourself with the DOM API. What have you tried so far? Commented Sep 15, 2021 at 8:16
  • 1
    Maybe he doesn't have control of the HTML produced? Maybe he's scraping a website and wants to transform the content in some way. Commented Sep 15, 2021 at 8:16
  • @JarneKompier because I use Clickfunnels, they don't give users the accessibility to hard code Commented Sep 15, 2021 at 8:17
  • You could try document.querySelectorAll('[value]') this will return an object with all the elements that have a value attribute. Commented Sep 15, 2021 at 8:30

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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

Even better you can use option:nth-child() to exclude other elements that might be the second under their parent
1

To change the text of the second option

document.getElementsByTagName('select')[0].options[1].innerHTML = "watch the webinar now";

If the option is not always second, you can give the option a id then use that in your query selector.

Comments

Your Answer

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.