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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:49:25+00:00 2026-06-03T08:49:25+00:00

Before explaining the problem I am facing,this is the code I am using: <!DOCTYPE

  • 0

Before explaining the problem I am facing,this is the code I am using:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.1/underscore-min.js"></script>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
        <script type="text/template" id="view_template">
            <li>
            <label class="lbl" style="width:100px;display:inline-block;"><%= name %></label>
            <button class="btn">Click</button>
            </li>
        </script>
        <script type="text/javascript">
            AppModel = Backbone.Model.extend({
                defaults : {
                    m_name : "Default name"
                },
                modify : function(value) {
                    this.set({
                        m_name : value
                    });
                }
            });

            $(document).ready(function() {
                $("#add").click(function() {
                    var appModel = new AppModel();
                    appModel.modify($("#txt").val());
                    new CustomView({
                        model : appModel
                    });
                    $("#txt").val("");
                });
                CustomView = Backbone.View.extend({
                    el : $(".list"),
                    events : {
                        "click .btn" : "showAlert"
                    },
                    initialize : function() {
                        this.render();
                    },
                    render : function() {
                        var variables = {
                            name : this.model.get("m_name")
                        };
                        var t = _.template($("#view_template").html(), variables);
                        $(this.el).append(t);
                    },
                    showAlert : function() {
                        alert(this.model.get("m_name"));
                    }
                });
            });

        </script>
    </head>
    <body>
        <input type="text" id="txt"/>
        <button id="add">
            Add
        </button>
        <ul class="list" style="list-style: none"></ul>
    </body>
</html>

A demo is here – http://jsfiddle.net/jCekv/

You can type any value into textfield and click Add.The text will be added to a list which is a backbone view.You can keep on adding multiple values to the list.After adding some items,click on the Click button in any of the items in the list.I want to get the value in the model for that view.But because of the way I am adding Events,event handlers are getting executed multiple times.For eg:If i add 3 items and click on any of the buttons in the list 3 alerts will come-one corresponding to each of the items that is added.This is not the way I want.I want to have the event being triggered only for the Backbone View which has been clicked so that I get access to the model of the Backbone View I have clicked.

How I can achieve this functionality?.Please let me know if my question is not clear.Thanks in advance.

  • 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-03T08:49:26+00:00Added an answer on June 3, 2026 at 8:49 am

    The problem is that all your CustomViews have their element set to $(".list"). Therefore, all delegate events are added to the same DOM element, the list, and not the individual list items.

    See a possible solution here: http://jsfiddle.net/mbuchetics/R8jPA/

    I changed the CustomView to use a tagName of li instead of setting the element. The template is not appended to the element but instead its content is set. This way, each CustomView represents one item of the list and the click event refers to exactly that item.
    I also removed the render call from initialize, instead the view is rendered when it is appended to the list (in the “add click” callback).
    Render() returns this and therefore you can use view.render().el to get the list item’s html.

    You could further improve your application to use collections and a separate view for the collection. The collection view could then handle events such as “add” on the collection and update itself (and the individual item views) accordingly.

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

Sidebar

Related Questions

I recently came across this web page http://www.yoda.arachsys.com/csharp/readbinary.html explaining what precautions to take when
This has some lengthy background before the actual question, however, it bears some explaining
Before I start explaining the code I will first give my use case so
Good evening everyone, before explaining my problem, I should give you some explanation on
I have been trying to ask this before, without any luck of explaining/proving a
Before I roll my own Queue using NSMutableArray , I'd like to know if
Before I begin I'll freely admit it's something in my code but it's not
Before I throw something ugly together I thought this would be a good one
Before the switch and in all apps on this passenger that run rails 2.0
Ok, I hope I've got everything listed up nicely before posting this question because

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.