Hit an element with Id in Javascript

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

How to do it?

+5
source share
4 answers

The easiest way to use vanilla javascript is to simply manipulate the contents of the HTML. It might look like this:

var targetElem = document.getElementById('myid');

targetElem.innerHTML = '<strike>' + targetElem.innerHTML + '</strike>';

Using jQuery, this task becomes a little more trivial 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 for more cross-browser matching:

$('#myid').css('text-decoration', 'line-through');
+5
source

sort of

document.getElementById('foo').style.textDecoration ='line-through';
+3
source

CSS

.strike{
    text-decoration:line-through;
}

JQuery

jQuery("#yourid").addClass("strike");
+2

yourID

, JQuery.

$("#yourID").css("textDecoration", "line-through");

JQuery.

var element = document.getElementById('yourID');
element.style.textDecoration = 'line-through';

, .

0

All Articles