+ 2
Help... Javascript replace text
<ul> <li>Apple red</li> <li>Banana</li> <\ul> -------------------------------- var newFruits = document.getElementsByTagName("li"); newFruits[0].replace("red","Green"); --------------------------------- but error, whats the problem ? i want 'apple red' to 'apple green'
3 Answers
+ 5
No, you guys get it wrong.
Zulfikar Ahmad your replace parameter has no problem.
But you cannot access the text from newFruits[0]
Change to this:
var newFruits = document.getElementsByTagName("li");
var fruit = newFruits[0].innerText;
newFruit = fruit.replace("red","Green");
newFruits[0].innerText = newFruit;
https://code.sololearn.com/WCsdZGzvZI1M/?ref=app
+ 3
Hello, Zulfikar Ahmad !
var newFruits = document.getElementsByTagName("li");
newFruits[0].replace(/red/ig, "Green");
Good luck in programming on SoloLearn!
https://www.sololearn.com/learn/9704/?ref=app
+ 2
Hello, Alexander Soxolov
Im using console firefox, but error.
The message is "replace is not a function"