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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:43:20+00:00 2026-06-08T10:43:20+00:00

I have a div generated by a backbone.js view. When the user clicks on

  • 0

I have a div generated by a backbone.js view. When the user clicks on this div, a class active is added to the div and the function addToSet is executed.

Problem: I want another function to be triggered when the View’s div has the class active. However, my attempt shown below always cause addToSet function to run when its clicked.

Now, I remove 'click': 'addToSet' from the events function, leaving only 'click .active': 'removeFromSet'. Clicking on the div does not cause anything to happen! Is this because the event handler cannot select the div of the view itself, just the elements inside it?

Any idea how I can solve this problem? Thanks!

JS Code

SetView = Backbone.View.extend({
    tagName: 'div',
    className: 'modal_addit_set',

    template: _.template( $('#tpl_modal_addit_set').html() ),

    events: {
        'click': 'addToSet',
        'click .active': 'removeFromSet'
    },

    initialize: function(opts) {
        this.post_id = opts.post_id;
    },

    render: function() {
        $(this.el).html( this.template( this.model.toJSON() ) );
        if(this.model.get('already_added'))
            $(this.el).addClass('active');
        return this;
    },

    addToSet: function() {
        $.post('api/add_to_set', {
            post_id: this.post_id,
            set_id: this.model.get('id'),
            user_id: $('#user_id').val()
        });
    },

    removeFromSet: function() {
        $.post('api/remove_from_set', {
            post_id: this.post_id,
            set_id: this.model.get('id')
        });
    }
});
  • 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-08T10:43:21+00:00Added an answer on June 8, 2026 at 10:43 am

    These events:

    events: {
        'click': 'addToSet',
        'click .active': 'removeFromSet'
    }
    

    don’t work and you sort of know why. From the fine manual:

    Events are written in the format {"event selector": "callback"}. The callback may be either the name of a method on the view, or a direct function body. Omitting the selector causes the event to be bound to the view’s root element (this.el).

    So your 'click': 'addToSet' binds addToSet to a click on the view’s el itself but 'click .active': 'removeFromSet' binds removeFromSet to a .active element inside the view’s el.

    I think the easiest solution is to have a single event:

    events: {
        'click': 'toggleInSet'
    }
    

    and then:

    toggleInSet: function() {
        if(this.$el.hasClass('active')) {
            $.post('api/remove_from_set', {
                post_id: this.post_id,
                set_id: this.model.get('id')
            });
        }
        else {
            $.post('api/add_to_set', {
                post_id: this.post_id,
                set_id: this.model.get('id'),
                user_id: $('#user_id').val()
            });
        }
    }
    

    You could use an instance variable instead of a CSS class to control the branching in toggleInSet if that makes more sense.

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

Sidebar

Related Questions

I have a div that contains HTML data (this changes and is user generated).
I have a DIV that contains user-generated contents. These contents may and will start
problem shown: http://test.aussieclubroom.com/test.html I have a div in which content is dynamically generated into.
I have the this piece of code here... <div class=pics2> <div style=position: absolute; top:
I have a list of dynamically generated div-tags with a class-attribute but no id.
I have dynamically generated code like the following: <div class=message-error error>The Message field is
I have a DIV tag generated from jQuery that goes like this: $('#results') .append('<DIV
I have div with class and id attributes in document like this: <div class=blabla11
week DKK id=radio2 value=75 /> I have bunch of these div entry generated by
I have a scrollable div element with a number of checkboxes dynamically generated. I

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.