Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear div before appending

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));
        });
   });
like image 319
et moi Avatar asked Feb 02 '16 07:02

et moi


People also ask

How do I clear a div content?

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!

How do I remove appended data?

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.

How to clear element in JavaScript?

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.

How remove and append in jQuery?

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.


2 Answers

You can remove elements from DOM using the remove method of jQuery:

$(".bookHolder").remove();
like image 147
erikscandola Avatar answered Nov 01 '22 12:11

erikscandola


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.

like image 41
Punit Gajjar Avatar answered Nov 01 '22 14:11

Punit Gajjar