-2

I need to replace the text English from option value with javascript. Code is next:

<div>
<select name="input_14" id="input_29_14">
<option value="English">English</option>
</select>
</div>

So this is what I tried:

document.querySelector("div.selector option[value=English]").text = "Inglés";

But it doesn't change the text. Anyone can provide me an alternative function?

Thank you

1
  • 1
    Your div has no class so div.selector won't work Commented Mar 26, 2018 at 15:52

3 Answers 3

3

Change your query selector to

document.querySelector("option[value=English]").text = "Inglés";
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Vadim, so how can I replace the text in another selector? I mean, this is replacing the text in one, but I have another dropdown box where the 'English' text appears and is not being replaced.
@Robertoss please share sample code with me.
is same code than above, just changes select id: <select name="input_14" id="input_29_91">
Hi, do you have any update for this? I just need to make this available to when English shows for second time.
@Robertoss HTML: <div> <select name="input_14" id="input_29_14"> <option value="English">English</option> <option value="English">English</option> </select> </div> JS: const elements = document.querySelectorAll("option[value=English]"); const length = elements.length; for (let i = 0; i < length; ++i) elements[i].text = 'Inglés';
|
1

The reason your current code is not working is because div.selector is looking for a div with the class selector.

Just add an id to the option, get the id with JavaScript and change to whatever you want.

document.getElementById("lang").text = "Anglais";
<div>
  <select name="input_14" id="input_29_14">
    <option id="lang" value="English">English</option>
  </select>
</div>

Comments

1

I solved it with jQuery:

jQuery("option[value=English]").text("Inglés");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.