How can I replace an element with Javascript in blogger? The element is a word in h2 post title. Example: Review | Armaggedon. I want to delete "Review |" How would be a script for do that?
Thank you.
How can I replace an element with Javascript in blogger? The element is a word in h2 post title. Example: Review | Armaggedon. I want to delete "Review |" How would be a script for do that?
Thank you.
You can make a javascript function (for example, see the function updateTitle
below) and call that whenever needed (or just do the following where needed). In that function, get a reference to the element using document.getElementById()
. Then using string.replace(), update the property innerHTML of that variable referencing the element.
In the example below, we call updateTitle
when the button is clicked, but it could be called in other scenarios, e.g. after loading content asynchronously (e.g. with AJAX techniques), other user interaction, etc.
function updateTitle() {
var theTitle = document.getElementById('theTitle');
theTitle.innerHTML = theTitle.innerHTML.replace('Review |','');
}
<h2 id="theTitle">Review | Armaggedon</h2>
<button onClick="updateTitle()">Update title</button>