javascriptbackbone.js

Create a button on click of a button


I have a button and I need to create more buttons when I pressed the button.

Here it is what I have written for the button:

<div class="response-container" id="response-container">
    <a href="#" class="button add-button" id="add-button">Add button</a>
</div>

I am trying to learn Backbone.js so I wrote a view to create more buttons when click add-button.

createButton = Backbone.View.extend({
    events: {
        'click .add-button': 'add_script'
    },
    add_script: function() {
        console.log('Pressed');
        //create a new button from here? 
     }
});

Can anyone help me to do it? console.log('Pressed') doesn't even work. I am trying to learn web development so, is there any document that you can suggest me? I am welcome for all suggestions in Backbone.js


Solution

  • You have to create an object of the view. Like this

    var createButton = Backbone.View.extend({
      el: '#response-container',
        events: {
            'click .add-button': 'add_script'
        },
    
        initialize: function() {
            console.log("initialized");
        },
        add_script: function() {
            console.log('Pressed');
            //create a new button from here? 
    
        this.$el.append('<input type="button" value="new button"/>');
    
    
         }
    });
    

    var cb = new createButton();

    Here is the DEMO