+ 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'

2nd Aug 2018, 4:20 AM
Zulfikar Ahmad
Zulfikar Ahmad - avatar
3 Respuestas
+ 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
2nd Aug 2018, 5:28 AM
Calviղ
Calviղ - avatar
+ 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
2nd Aug 2018, 4:55 AM
Alexander Sokolov
Alexander Sokolov - avatar
+ 2
Hello, Alexander Soxolov Im using console firefox, but error. The message is "replace is not a function"
2nd Aug 2018, 5:22 AM
Zulfikar Ahmad
Zulfikar Ahmad - avatar