Let suppose I have a string:
var firstString = "<h3>askhaks</h3><h3>1212</h3><h1 style='color:red;
text-decoration:underline;'><a href=''><span id='123'><i class='fa fa-inr'></i>
</span> Hello! Admin</span></a></h1><p>This is the content of page 'String
Replace in Javascript'</p><h1>First</h1><span><h1>Hello! Admin</h1>Thank You for
visiting this page</span><h1>Third</h1>";
I want to change text of first <h1> tag without losing all other inner tags i.e. <a href=''><span id='123'><i class='fa fa-inr'></i> </span>
Just want to replace Hello! Admin with another text. I am able to replace text of first <h1> tag with the below code without losing the inline styling added to <h1> but I am loosing the inner tags.
var innerText = document.getElementsByTagName('h1')[0].innerHTML;
How to achieve this?
document.getElementsByTagName('h1')it seems like you have an element in a document, not just a string? Which is it?<span id="h1-test">Hello! Admin</span, then you can retrieve it and modify it to your heart's content.