-1

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.

Sunny
  • 1
  • 1
  • http://www.w3schools.com/js/js_htmldom_elements.asp – StackOverMySoul Oct 12 '16 at 15:39
  • document.getElementById("YOUR_H2_ID").innerText.replace("Review |", "") – Zeph Oct 12 '16 at 15:40
  • Thank you. How I can made a Script with this? Sorry for the question I don't know nothing about JVScript. – Sunny Oct 12 '16 at 15:47
  • 1
    Possible duplicate of [How to replace DOM element in place using Javascript?](http://stackoverflow.com/questions/843680/how-to-replace-dom-element-in-place-using-javascript) – Liam Oct 12 '16 at 15:55

1 Answers1

0

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>
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • So, how can I make this but with a class. (.post h1, for example)? – Sunny Oct 12 '16 at 16:07
  • does your h1 have (only) an attribute `class="h1"`? if so, are there multiple elements with that classname and would you want to update more than one? if it is just one, then add the `id` attribute and use that – Sᴀᴍ Onᴇᴌᴀ Oct 12 '16 at 16:10