Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
How to replace an HTML element's text with JavaScript when the element doesn't have a class?

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"

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

What about selecting it by using its DOM order?

var perent = 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?

about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!