+ 2
How to replace a word in <p>
HTML: <p id="text">Hello world!</p> How can I replace "world" to "user"?
3 Respuestas
+ 6
First you have to select the p element.
You can use innerHTML property for changing the whole text.
But as you asked how to change world to user, you can split the the whole text, store it in an array, then can change the the world to user using if statement.
Alternatively you can wrap the world text in another element like span, then select the same and change it with innerHTML.
+ 6
You can also retrieve the <p> textContent or innerHTML (it's a string) then use .replace() method to replace the first 'world' substring only, or .replaceAll() to replace all 'world' substring.
0
InnerHtml is a dom property can change this very easily using events