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 8881411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:20:33+00:00 2026-06-14T20:20:33+00:00

Iam trying to build a very simple ul where u have a input box

  • 0

Iam trying to build a very simple ul where u have a input box and add button , on clicking add button , text from input is appended to ul

this is my code :

HTML :

<body>
<input type="text" id="name">
<button id="add">Add</button>
<ul id="mylist"></ul>

JS :

$(function(){

var myCollection = Backbone.Collection.extend();

var myView = Backbone.View.extend({

    el:$('body'),

    tagName:'li',

    initialize : function(e){
        this.collection.bind("add",this.render,this);
    },

    events:{
        'click #add' : 'addfoo',
        'click .button':'removefoo'
    },

    addfoo:function(){
       var myname= $('#name').val();
       $('#name').val('');
       console.log('name entered: '+myname);
       this.collection.add({name:myname});
    },

    removefoo:function(){
             //need the code here 
    },

    render:function(){

        $('#mylist').empty();
        this.collection.each(function(model){
            $('#mylist').append('<li>'+model.get('name') + "<button class='button'>"+"delete"+"</button></li>");
        });

    }

});
var view = new myView({collection: new myCollection()});
    });

My add funcitonality is working , but when i click the button for delete , which model from collection should be deleted , iam stuck there , please help me out.Just need the code , for what do delete from collection in removefoo function.

In other word how do i get which model to be removed when button is clicked

Thank you

  • 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-14T20:20:34+00:00Added an answer on June 14, 2026 at 8:20 pm

    I think you need a more modular approach to Backbone, let me explain.

    Backbone is a way to organize your code. Having just one Backbone view do it all, doesn’t change a lot.

    Instead, try to see what views you actually need:

    • MainView
    • ListView
    • ListItemView

    MainView could look like this:

    var MainView = Backbone.View.extend({
    
        el: 'body',
    
        initialize : function(options) {
           this.collection = new Backbone.Collection.extend({ url: '/items' });
        },
    
        events:{
        },
    
        render:function(){
            var listView = new ListView({ el: this.$("#myList") });
            listView.render();
    
            this.collection.fetch();
    
            return this;
        }
    });
    

    ListView

    var ListView = Backbone.View.extend({
    
        tagName: 'ul',
    
        initialize : function(options) {
            _.bindAll(this, "render");
    
            this.collection.on("add", this.appendListItem, this);
        },
    
        events:{
        },
    
        render: function() {
            this.collection.each(this.appendListItem, this);
    
            return this;
        },
    
        appendListItem: function (model, collection, options) {
            var listItem = new ListItemView({ model: model});
            this.$el.append(listItem.render().el);
        }
    });
    

    ListItemView

    var ListItemView = Backbone.View.extend({
    
        tagName: 'li',
    
        initialize : function(options) {
            _.bindAll(this, "render");
    
            this.model.on("destroy", this.remove, this);
        },
    
        events:{
            "click button": "delete"
        },
    
        render:function(){
            this.$el.text(this.model.get('name'));
            this.$el.append("<button class='button'>Delete</button>");
    
            return this;
        },
    
        delete: function (event) {
            this.model.destroy();
        }
    });
    

    Kick off the main view: var view = new MainView().render();

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

Sidebar

Related Questions

I am trying to build a very simple Contact browser. I have a Collection
I am trying to build this very simple (visually speaking) layout using HTML/CSS that
I'm trying to wrap my head around MSBuild. I have a very simple script
I am trying to build a very simple price comparison script. Until now, I
I have built a simple registration form shown below and Iam trying to get
I am trying to build a very simple C++ program using the Maven NAR
I am trying to build a very simple java project in Eclipse SDK Version:
I am trying to do a very simple thing, build an URL for a
I am trying to build a very simple C++ unit test project. The setup
I am trying to build something very similar to like this using django activity

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.