Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:17:05+00:00 2026-06-17T23:17:05+00:00

In my backbone function, how can i remove the elements before i append new

  • 0

In my backbone function, how can i remove the elements before i append new elements?, my function all works fine. the issue is while i click on the ‘update’ like, it’s updating the elements and still the older version available. how can i remove the older elements, and append new from update call..?

what is the correct way to clear the existing view elements and append new collections?

my function:

$(document).ready(function(){

    var school = {};

    school.model = Backbone.Model.extend({
        defaults:{
            name:'no name',
            age:'no age'
        }
    });

    school.collect = Backbone.Collection.extend({
        model:school.model,
        url:'js/school.json'
    });

    school.view = Backbone.View.extend({
        tagName:'div',
        className:'member',
        template:$('#newTemp').html(),
        render:function(){
            var temp = _.template(this.template);
            this.$el.html(temp(this.model.toJSON()));
            return this;
        }
    });

    school.views = Backbone.View.extend({
        el:$('#content'),
        events:{
            'click #newData' : 'newArrival',    
        },
        initialize:function(){
            _.bindAll(this);
            this.collection = new school.collect;
            this.collection.bind('reset', this.render);
            this.collection.fetch();
        },
        newArrival:function(){
            school.views.remove(); // i tried this, throw the error 
            this.initialize();
        },
            render:function(){
                var that = this;
                _.each(this.collection.models, function(item){
                    that.renderItem(item);    
                })
            },
            renderItem:function(item){
                //how can i remove the older elements and append new alone?
                var newItem = new school.view({model:item});
                this.$el.append(newItem.render().el); // keep adding but previous element still not removed, 
            }
        });

        var newSchool = new school.views;

})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:17:06+00:00Added an answer on June 17, 2026 at 11:17 pm

    You need to have a reference to you old views to be able to remove them. You will then be able to to call remove on the child views. I highly recommend that you use backbones remove method instead of just removing elements from the DOM with jquery. Using Backbones remove will also unbind events from the object. To update from server, just call collection.fetch() to get new data. This is my solution (jsfiddle at the bottom):

    var school = {};
    
    school.model = Backbone.Model.extend({
        defaults:{
            name:'no name',
            age:'no age'
        }
    });
    
    school.collect = Backbone.Collection.extend({
        model:school.model,
        url:'js/school.json'
    });
    
    school.view = Backbone.View.extend({
        tagName:'div',
        className:'member',
        template:$('#newTemp').html(),
        render:function(){
            var temp = _.template(this.template);
            this.$el.html(temp(this.model.toJSON()));
            return this;
        }
    });
    
    school.views = Backbone.View.extend({
        el:$('#content'),
        events:{
            'click #newData' : 'newArrival',    
        },
        initialize:function(){
            _.bindAll(this);
            this.collection = new school.collect;
            this.collection.bind('reset', this.render);
            this.collection.fetch();
    
            this.childViews = [];
        },
        newArrival:function(){
            this.collection.fetch();
        },
        render:function(){
            var that = this;
    
            // Remove old items
            _.each(this.childViews, function(old){
                old.remove();    
            });
    
            // Empty array of children
            this.childViews = [];
    
            var that = this;
            _.each(this.collection.models, function(item){
                that.renderItem(item);    
            });
        },
        renderItem:function(item){
            //how can i remove the older elements and append new alone?
            var newItem = new school.view({model:item});
            this.$el.append(newItem.render().el); // keep adding but previous element still not removed, 
    
            // Store a reference to your child item.
            this.childViews.push(newItem);
        }
    });
    
    var newSchool = new school.views;
    

    I have created a working: JSFiddle. Note: In the JSFiddle collection I override fetch() to provide fake data (You should remove that part).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my backbone function, all works fine, even the filter. but the issue is,
I can successfully do this: App.SomeCollection = Backbone.Collection.extend({ comparator: function( collection ){ return( collection.get(
The View.remove() function in backbone js removes the container element of the view itself
Hey all I am pretty new to Backbone though I have put several days
How can I call the super's render function in backbone (coffeescript)? If not in
How can i add click event to my el elements in this case multiple
First I create some backbone views: (function() { var SomeView = Backbone.View.extend({ ... });
I want to run a function (to render flash messages) after a backbone.navigate call.
var JavascriptHelper = Backbone.Model.extend(JavascriptHelper, {}, // never initialized as an instance { myFn: function()
I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.