How to get the element that raised the event through the trunk event handler

I have an idea.

//define View var CreatePollView = Backbone.View.extend({ events: { "click #addOption": "addOption", "change .dynamicInput": "changeInputs" }, initialize: function () { _.bindAll(this, "render", "addOption", "changeInputs"); this.model.bind('change', this.render); }, changeInputs: function () { var newVal = $(this).val(); // this throws exception in jquery script this.model.set("Subject", { Subject: newVal }); }, .... 

How can I access the element (this is an input element) on which the change event was triggered?

+7
source share
2 answers

You get an exception because you are calling _.bindAll on changeInputs . When you do this, you say that changeInputs will be bound to your object when it is called.

In other words, when you reference $(this) , you send an instance of CreatePollView to jQuery, which he doesn't like.

You want to keep this binding, though, since you are accessing your model ( this.model ), therefore this should be an instance of CreatePollView .

Instead, you can receive an event from your function and use target or other information:

 changeInputs: function(e) { } 
+8
source
 changeInputs: function(e){ //use e.target ... } 
0
source

All Articles