1

I'm trying to call view function in when clicking a button, I found this question and followed but still not reach the view function.

Html:

<form class="addToCart-form">
    <input type="button" id="addToCart" class="addToCart" value="Add To Cart" />
</form>

View :

var CartView = Backbone.View.extend({
   events : {
       "click : #addToCart" : "addToCart"
   },
   addToCart : function (){
        console.log('saved');
        return false;
   },
   render: function(){
      $("#list").html("string");
       $("#rightpanel").html("another string");
   }
});

I tried console.log in my router and render() are working, except in addToCart function.

Any help would be appreciated.

Community
  • 1
  • 1
Nothing
  • 2,644
  • 11
  • 64
  • 115

1 Answers1

0

You need to assign el value to this view first for example

 var CartView = Backbone.View.extend({
   el: '.addToCart-form', 
   events : {
       "click : #addToCart" : "addToCart"
   },
   addToCart : function (){
        console.log('saved');
        return false;
   },
   render: function(){
      $("#list").html("string");
       $("#rightpanel").html("another string");
   }
});

After that you can add event handler like you did on the basis of element id (Better approach is to user element class to attach events). Then your will be able to access your form elements like

this.$el.find('#addToCart')
umar
  • 910
  • 10
  • 24