Im currently unable to clear the container div before appending new elements. (The whole div is wiped clean when empty()
, i dont want this. I simple want to remove the existing book holder elements. Is there something I'm doing it wrong? and/or could this because of the "text/template" script?)
HTML snippet
<div class="component">
<ul id="Maincontainer" class="align">
<script type="text/template" id="template">
<li id="bookHolder" class="bookHolder">
<figure class="book">
<!-- Front -->
<ul class="hardcover_front">
<li>
<img src="{{imgURL}}" alt="" width="100%" height="100%">
</li>
<li></li>
</ul>
</figure>
</li>
</script>
</ul>
</div>
JQUERY snippet
$.get( "URL", function(data) {
var entry = $(data).find("entry");
entry.each(function() {
var template = $("#template").html();
//$("#Maincontainer").remove(); - >clear whole div, appended elements not show
//$("#Maincontainer").remove("#bookHolder"); -> Div not cleared
$("#Maincontainer").append(template.replace("{{bookName}}",
bookName).replace("{{summary}}", summary).replace("{{price}}", price).replace("{{seller}}", artist).replace("{{imgURL}}", image)
.replace("{{href}}", href).replace("{{category}}", category));
});
});
To clear the contents of a div element, set the element's textContent property to an empty string, e.g. div. textContent = '' . Setting textContent on the element removes all of its children and replaces them with a single text node of the provided value. Copied!
Approach 1: Initially use removeClass() method to remove active class of previously appended data menu item. Then followed by addClass() method to add active class of currently appended data menu item. Now use each() function to append data with respect active class added or removed on click.
In JavaScript, an element can only be deleted from its parent. To delete one, you have to get the element, find its parent, and delete it using the removeChild method.
jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.
You can remove elements from DOM using the remove method of jQuery:
$(".bookHolder").remove();
If you want to remove the entire dive then you can use
$(".bookHolder").remove();
Or if you just want to append some data to $(".bookHolder") then you should clear the dive before like you need to remove the div content, and for that you can use
$(".bookHolder").html("");
which will clear the the entire div content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With