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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:48:16+00:00 2026-05-30T00:48:16+00:00

I have a toggle event that is being used to show and hide a

  • 0

I have a toggle event that is being used to show and hide a div and it is hooked up inside a backbone view and called via an event delegate. On the first click of the link toggle() is skipped. On the second and third click toggle() is called as expected.

Any ideas how I can get the toggle event working on first click?

events:{
    "click a.docSectionHeading" : "Expand" 
  },

  initialize: function(options) {
    _.bindAll(this, "render", **"Expand"**);
    this.model.bind("change", this.render);      
    this.model.fetch();
  },

  render: function() {    
    var $el = $(this.el);
    $el.empty();
    $el.append(this.template.to_html({
      message: this.model.get("message")  
    }));

    return this;
  },

  Expand: function() {

    var tempid = "";
    var id = "";

    // Not called on first click
    $("a.docSectionHeading").toggle(
      function () {

        tempid = $(this).attr("data-id");
        id = tempid.replace(".", "\\.");

         // show -, hide +
        $("img#doc_minus_" + id).removeClass(".noShow");
        $("img#doc_minus_" + id).show();
        $("img#doc_plus_" + id).hide();

        // show clicked section.
        $("#" + id).show();
      },

      function(){
        tempid = $(this).attr("data-id");
        id = tempid.replace(".", "\\.");

         // show -, hide +
        $("img#doc_minus_" + id).addClass(".noShow");
        $("img#doc_minus_" + id).hide();
        $("img#doc_plus_" + id).show();

        // show clicked section.
        $("#" + id).hide();
      }
    )

    return false;
  }
  • 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-05-30T00:48:17+00:00Added an answer on May 30, 2026 at 12:48 am

    jQuery toggle binds the click-toggle behavior so the first time you click it the event for handling toggle gets bound but doesn’t get triggered as it wasn’t bound yet and nothing happens, that’s why it works on the second click. Another problem here is that now every time you click you bind the event again and again which will make the event trigger multiple times over time and it can produce all different kind of hard to track errors + performance will suffer.

    To fix that you need to bind the toggle event in the render method after you’ve rendered the template or create a helper method bindToggle or something like that and call it from render method after you’ve rendered the template.

    edit: and some tips

    • it’s a broadly accepted standard that we start name of the methods with lowercase and name of classes/constructors with uppercase – this might be confusing for other programmers.
    • you use empty and then append – it is an equivalent of jquery html it will empty and then append your html/DOM nodes
    • if you bind an event to element you don’t need to query for it again you can get it from the event object using $(event.target)
    • if you are using Backbone 0.9+ you don’t need to use $(this.el) to get jquery object representing the element you can just use cached copy of the jquery wrapped element by accessing this.$el
    • also if the element you are querying for is a child of the current view’s element then it’s more efficient to use the backbone this.$ method (this.$('a.docSectionHeading')) as it is much more efficient as this will search for the element only in the children of the current element. Also it allows you to query for elements which weren’t added to the document DOM tree yet.

    ps. and hope that the **"Expand"** in the bindAll is just an error? what are the stars for?

    Fixed code:

    initialize: function(options) {
      _.bindAll(this, "render");
      this.model.bind("change", this.render);      
      this.model.fetch();
    },
    
    render: function() {    
      var $el = $(this.el);
      $el.empty();
      $el.append(this.template.to_html({
        message: this.model.get("message")  
      }));
    
      this.bindExpand();
    
      return this;
    },
    
    bindExpand: function() {
    
      var tempid = "",
          id = "";
    
      this.$("a.docSectionHeading").toggle(
        function () {
    
          tempid = $(this).attr("data-id");
          id = tempid.replace(".", "\\.");
    
           // show -, hide +
          $("img#doc_minus_" + id).removeClass(".noShow");
          $("img#doc_minus_" + id).show();
          $("img#doc_plus_" + id).hide();
    
          // show clicked section.
          $("#" + id).show();
        },
    
        function(){
          tempid = $(this).attr("data-id");
          id = tempid.replace(".", "\\.");
    
           // show -, hide +
          $("img#doc_minus_" + id).addClass(".noShow");
          $("img#doc_minus_" + id).hide();
          $("img#doc_plus_" + id).show();
    
          // show clicked section.
          $("#" + id).hide();
        }
      );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a toggle button, with a show/hide feature in javascript. How do I
I have a jquery toggle that animates and displays a DIV. I have a
I have a show/hide toggle working well in multiple instances (thanks to help here
I have a div that I want to toggle between displaying or not when
I have a toggle event on specific div witch works fine , here is
I have some HTML that I need to toggle between two structures - one
Ok so I have a div that contains a few forms that have dynamically
I have a gridview that is populated on the Page_Load event if !PostBack is
I have a number of table rows that I would like to toggle the
So I have been creating a simple animation that copies a div, and expands

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.