How to do strike through string for javascript

SS SS picture SS SS · Aug 17, 2013 · Viewed 10.6k times · Source

I want to strike through text in Javascript but I can't seem to get the code to work.

var message = document.getElementById('helloWorld');
setTextContent(message, 'hello world!'.strike());

Would appreciate any help. Would also like to do it without using css.

Should Also mention that these lines of code are inside another function called totalPackage() which runs when the user clicks a button. I want my message hello world! to be displayed when this other function is called.

Answer

laggingreflex picture laggingreflex · Dec 18, 2018

Using Unicode Character 'COMBINING LONG STROKE OVERLAY' (U+0336)

E̶x̶a̶m̶p̶l̶e̶

function strikeThrough(text) {
  return text
    .split('')
    .map(char => char + '\u0336')
    .join('')
}
<input oninput="document.querySelector('#output').innerText = strikeThrough(this.value)" placeholder="type here"><p id="output"></p>

To undo it, simply remove all the \u0336 characters from the string.

string.replace(/[\u0336]/g, '')