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.
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.
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.
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!
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, '')
try this
var message = document.getElementById('helloWorld');
message.innerHTML='<del>helloworld</del>'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With