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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:49:56+00:00 2026-06-09T11:49:56+00:00

In Backbone.js I am trying to use a variable as a selector to bind

  • 0

In Backbone.js I am trying to use a variable as a selector to bind to an event like this:

events: function() {
    var _events = {};
    _events["click " + "#plus-" + this.options.count] = "add";
    return _events;
}

Some (or more than one) thing must be wrong because Backbone seems to ignore it.

Most examples I’ve seen here use class and not id selectors. Is there a reason for that?

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-09T11:49:58+00:00Added an answer on June 9, 2026 at 11:49 am

    I have to say that I don’t understand this dynamic events declaration need. But I suppose you have your reasons.

    Any how I’ve come up with a working solution that looks like this:

    // html
    <div id="container">
        <button id="button-1">button 1</button>
        <button id="button-2">button 2</button>
    </div>​
    
    // js
    var View = Backbone.View.extend({
        events: function(){
            var _events = {};
            _events["click " + "#button-" + this.options.count] = "buttonClick";
            return _events;
        },
        buttonClick: function(){
            console.log( "click() on button " + this.options.count );
        }
    });
    
    var view1 = new View({
        el: $('#container'),
        count: 1
    });
    
    
    var view2 = new View({
        el: $('#container'),
        count: 2
    });
    

    Check the working jsFiddel

    So, either I’m missing something or the issue is somewhere else.

    About using class or id in your events css selector it is just up to you.

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

Sidebar

Related Questions

I'm trying to use Nodeunit to test my Backbone models and later I'd like
I am trying to use the backbone architecture in my MVC application. I created
I'm trying to set up a Backbone application. This code below gives to following
I'm trying to use CoffeeScript with Backbone.js (via Brunch) and I want to implement
Trying to get my head around backbone.js. This example is using Backbone Boilerplate and
im starting to use Backbone.js and im trying to do somthing simple with javascript,
I'm trying to write my first mixin for use across different backbone projects, and
What is the best way to bind events to a Backbone boilerplate application? I've
I'm trying to use backbone.js with underscore.js templates in an asp.net web forms application
I'm trying to use Backbone.js to in a JQuery Dialog. I've managed to get

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.