ojek
ojek

Reputation: 10068

Javascript - clearing everything inside a div

I have div:

<div id="socialUserList">
//some content here, htmlTags, text, etc.
</div>

Now, I want everything inside of that div to be wiped out. I am trying this:

$("#socialUserList").innerHTML = '';

But for some reason it doesn't want to work. Why?

Upvotes: 3

Views: 87

Answers (2)

user1467267
user1467267

Reputation:

The normal JavaScript method:

document.getElementById('socialUserList').innerHTML = '';

In jQuery:

$('#socialUserList').html('');

Pure JavaScript and jQuery go hand in hand, like so:

From pure JavaScript to jQuery:

var socialUserList = document.getElementById('socialUserList');
console.log($(socialUserList).html());

From jQuery to pure JavaScript:

var socialUserList = $('#socialUserList');
console.log(socialUserList[0].innerHTML);

Upvotes: 7

Eric S
Eric S

Reputation: 1363

Have you tried:

jQuery('#socialUserList').empty();

Note: You may have also tried this:

jQuery('@socialUserList')[0].innerHTML = '';

Using the [0] will access the DOM object of the first matching element.

Upvotes: 1

Related Questions