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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:24:53+00:00 2026-06-01T09:24:53+00:00

I have backbone view that binds an action to an element in the DOM.

  • 0

I have backbone view that binds an action to an element in the DOM. The problem is that the DOM element hasn’t been rendered when the events is called. My code looks like this:

  // Change the focus to a full goal view
  var GoalFullView = Backbone.View.extend({

    // Bind the goal full view
    el: $("#main"),

    // Events for making changes to the goal
    events: {
      "keypress #goal-update": "createOnEnter",
    },

    // Cache the template for the goal full view
    template: _.template($("#goal-full-template").html()),

    // Render the goal full and all of its goal updates
    initialize: function() {
      this.render();
      this.collection = new GoalUpdateList;
      this.collection.bind('add', this.addOne, this);
      this.collection.bind('reset', this.addAll, this);

      this.collection.fetch();
    },

As you can see, the event binding occurs before the initialization so it cannot locate the DOM element because it hasn’t been rendered. How can I delay the event binding until after initialize?

Edit 1:
Hmm, I do a console log, and it prints something out on a keypress, but I am getting this message in my Chrome’s developer console:

Uncaught TypeError: Cannot call method 'apply' of undefined

What does this mean?

  • 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-01T09:24:54+00:00Added an answer on June 1, 2026 at 9:24 am

    As I expected the problem is in another place.

    The DOM events binding declaration can be done before the DOM element real exists.

    I think this code example reproduces the situation you are talking and still the DOM event is binded correctly.

    var View = Backbone.View.extend({
      events: {
        "click #link": "click"
      },
    
      click: function(){
        console.log( "click event" );
      },
    
      render: function(){
        this.$el.html( "<a href='#' id='link' onclick='return false'>link</a>" );
      }
    });
    
    new View({ el: "body" }).render();​
    

    Check the jsfiddle example code

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

Sidebar

Related Questions

I have the following backbone.js code and i have a problem in that event
I have a Backbone View that has a Collection as its Model. If the
I'm having an issue with backbone.js' models. I have a view that creates another
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
I have a backbone view that calls to a sub-view: lr.MapView = Backbone.View.extend({ el:
I have a backbone app with a view structure that looks like the following
I have a case that uses view inheritance, and my code looks essentially like:
I'm using Backbone.js have a segmented control-type UI element for each model's view. They
I have a Backbone View that uses iScroll to implement a slideshow. iScroll publishes
I have some Backbone.js code that bind a click event to a button, and

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.