Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do strike through string for javascript

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.

like image 985
SS SS Avatar asked Aug 17 '13 05:08

SS SS


People also ask

How do you strikethrough text in HTML?

The <s> HTML element renders text with a strikethrough, or a line through it. Use the <s> element to represent things that are no longer relevant or no longer accurate. However, <s> is not appropriate when indicating document edits; for that, use the <del> and <ins> elements, as appropriate.

What is a strike through text?

A strikethrough is a horizontal line drawn through text, used to indicate the deletion of an error or the removal of text in a draft.

How do you strikethrough in react?

Use the textDecoration property to strikethrough text in React, e.g. <span style={{textDecoration: 'line-through'}}> . The text-decoration CSS property sets the appearance of decorative lines on text. Copied!


2 Answers

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, '')
like image 135
laggingreflex Avatar answered Oct 02 '22 10:10

laggingreflex


try this

var message = document.getElementById('helloWorld');
message.innerHTML='<del>helloworld</del>'
like image 34
Amith Avatar answered Oct 03 '22 10:10

Amith