Remove innerHTML from div

JqueryInnerhtml

Jquery Problem Overview


I'm trying to clear the div's innerHTML before repopulating it. I tried removeData() but once that's called, when I try to add the data, I get nothing from the next line after remove whereas if I remove the removeData() it's fine again. I just want to clear out any previous content in that div before I re-populate it.

    divToUpdate.removeData(); //clean out any existing innerHTML div content first
    divToUpdate.html(data);

It looks like it never gets to my divToUpdate.html(data) for some reason after it calls that removeData();

Jquery Solutions


Solution 1 - Jquery

jQuery Data is a different concept than HTML. removeData is not for removing element content, it's for removing data items you've previously stored.

Just do

divToUpdate.html("");

or

divToUpdate.empty();

Solution 2 - Jquery

To remove all child elements from your div:

$('#mysweetdiv').empty();

.removeData() and the corresponding .data() function are used to attach data behind an element, say if you wanted to note that a specific list element referred to user ID 25 in your database:

var $li = $('<li>Joe</li>').data('id', 25);

Solution 3 - Jquery

$('div').html('');

But why are you clearing, divToUpdate.html(data); will completely replace the old HTML.

Solution 4 - Jquery

divToUpdate.innerHTML =     "";   

Solution 5 - Jquery


var $div = $('#desiredDiv');
$div.contents().remove();
$div.html('<p>This is new HTML.</p>');

That should work just fine.

Solution 6 - Jquery

you should be able to just overwrite it without removing previous data

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPositiveGuyView Question on Stackoverflow
Solution 1 - JquerywompView Answer on Stackoverflow
Solution 2 - JqueryAnnika BackstromView Answer on Stackoverflow
Solution 3 - JqueryChristopher AltmanView Answer on Stackoverflow
Solution 4 - JqueryplodderView Answer on Stackoverflow
Solution 5 - JqueryJames SumnersView Answer on Stackoverflow
Solution 6 - JquerymateiView Answer on Stackoverflow