Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strike the Element using Id in Javascript

I need to strike all the text based on the element id using javascript.

How to do this?

like image 775
Vignesh Babu Avatar asked Jul 27 '11 08:07

Vignesh Babu


People also ask

How do I grab an element by ID?

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.

How do you strikethrough in JavaScript?

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.


2 Answers

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');
like image 178
jAndy Avatar answered Oct 10 '22 17:10

jAndy


something like

document.getElementById('foo').style.textDecoration ='line-through';
like image 20
amal Avatar answered Oct 10 '22 18:10

amal