I need to strike all the text based on the element id using javascript.
How to do this?
Document.getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
To create a strikethrough text with JavaScript, use the strike() method. This method causes a string to be displayed as struck-out text as if it were in a <strike> tag.
The easiest way to go using vanilla javascript should be just to manipulate the HTML content itself. This could look like:
var targetElem = document.getElementById('myid');
targetElem.innerHTML = '<strike>' + targetElem.innerHTML + '</strike>';
Using jQuery, this task becomes only slightly more trivial by using .contents()
+ .wrapAll()
:
$('#myid').contents().wrapAll('<strike/>');
Another alternative, using css might also be an idea:
targetElem.style.textDecoration = 'line-through';
Or again using jQuery to be more cross-browser compliant:
$('#myid').css('text-decoration', 'line-through');
something like
document.getElementById('foo').style.textDecoration ='line-through';
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