Skip to content Skip to sidebar Skip to footer

How To Clear A Span Inside A Div

I want to clear a value of a span inside a div without deleteing other contents only the contents of a span. ex: body:
one &l

Solution 1:

If you were using JQuery ( which will bring you much joy ), then you could do it like this.

$("span").html("");

Solution 2:

If you're only using pure JS, you can try this:

document.getElementById("content").getElementsByTagName("span")[0].innerHTML="";

You can replace 0 to any valid index to clear certain span.

Solution 3:

Alright, I lied I guess. This is more simpler than I thought, thanks to @Passerby;

for (i = 0; i < document.getElementById('content').getElementsByTagName('span').length; i++) {
    document.getElementById('content').getElementsByTagName('span')[i].innerHTML = '';
}

Solution 4:

Using jQuery: To target only the span that are in your div with id = "content" (in case you have other spans in your document:

$('div#content').find('span').html('');

Post a Comment for "How To Clear A Span Inside A Div"