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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:31:16+00:00 2026-05-27T10:31:16+00:00

I am new to Backbone.js. I have created a View for a button that

  • 0

I am new to Backbone.js. I have created a View for a button that already exists on the page. I apply jquery-ui to the button when the view is initialized:

var ButtonView = Backbone.View.extend({
    el: $('#ButtonId'),
    initialize: function () {
        $(this.el.selector).button();
    }
});

This method works, but I am wondering if there a simpler selector I could use rather than $(this.el.selector)? At first I thought I would be able to just do el.button(); but that does not work.

Or is there a better/simpler way to approach the whole process?

  • 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-27T10:31:17+00:00Added an answer on May 27, 2026 at 10:31 am

    i can assure you el.button(); works
    but…

    you need to keep in mind when your view jquery selector is executed.

    when you type it like you did, your jQuery selector is executed when the view code is parsed,
    meaning, this mostly happens when your dom was not ready loading.

    you can best pass the el in later like this:

    var ButtonView = Backbone.View.extend({
        initialize: function () {
            this.el.button();
        }
    });
    
    $(function(){
        var myButton = new ButtonView({ el: $('#ButtonId') });
    });
    

    now, your view is instantiated on document ready, the button element is passed because the dom was ready and your selector resolves to the button element. and you can happily use el.button(); to apply something on it.

    example: http://jsfiddle.net/saelfaer/KEQQB/

    edit

    another way to do it is to define your elementselector as the el argument, and only execute it like you did.

    var ButtonView = Backbone.View.extend({
        el: '#ButtonId'
        initialize: function () {
            $(this.el).button();
        }
    });
    
    $(function(){
        var myButton = new ButtonView({ });
    });
    

    this will work as well, but you would only be able to use this view on your given button unless you override it, the first example is cleaner for reusing code, you could instantiate it 3 times and each of them you could pass another button to…

    example: http://jsfiddle.net/saelfaer/KEQQB/6/

    sidenote
    on a sidenote i’d suggest using the render method, you are free to do this in the init, but i believe the render method is the one that should be handling this.

    var ButtonView = Backbone.View.extend({
    
        initialize: function () {
            _.bindAll(this, "render");
        },
    
        render: function () {
            this.el.button();
            return this;
        }
    });
    
    $(function(){
        var myButton = new ButtonView({ el: $('#ButtonId') }).render();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: var SomeNamespace = { Model: Backbone.Model.extend(), View: Backbone.View.extend({ model: new this.Model,
I have created a new backbone.js widget model which I hope to extend: var
I have created a model in backbone.js. var Content = Backbone.Model.extend({ // If you
I have a generic subclass of Backbone.View which has a close event listener. var
I have a view that displays items in a grid. If a button to
I have a backbone view - which when called presents a form $('#add-offer').click(function() {
In the code below I have two jQuery selectors that seem to work correctly.
I have a view and collection like this: window.DmnView = Backbone.View.extend({ template: _.template($(#tmpl_dmnListItem).html()), events:
I have a view that creates and populates a Selectlist. I want to bind
TL;DR Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype) the proper way to have a Backbone View

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.