I wonder why the this or $(this) selector isnt working when attaching functions to events in Backbone js. Look at this example code:
var testView = Backbone.View.extend({
el: $('#test'),
events: {
'keyup #signup-fullname': 'validateFullname'
},
validateFullName: function(e){
if($(this).val() == "mike"){
alert('You are just amazing!');
} else if($(this).val() == "tom"){
alert("mmm.. you fail...")
}
}
});
It is not working and it only works if I do this:
var testView = Backbone.View.extend({
el: $('#test'),
events: {
'keyup #signup-fullname': 'validateFullname'
},
validateFullName: function(e){
if($('#signup-fullname').val() == "mike"){
alert('You are just amazing!');
} else if($('#signup-fullname').val() == "tom"){
alert("mmm.. you fail...")
}
}
});
Isn't that a bit of an overkill can it be done with this or $(this)?
Thanks
I wonder why the this or $(this) selector isnt working when attaching functions to events in Backbone js. Look at this example code:
var testView = Backbone.View.extend({
el: $('#test'),
events: {
'keyup #signup-fullname': 'validateFullname'
},
validateFullName: function(e){
if($(this).val() == "mike"){
alert('You are just amazing!');
} else if($(this).val() == "tom"){
alert("mmm.. you fail...")
}
}
});
It is not working and it only works if I do this:
var testView = Backbone.View.extend({
el: $('#test'),
events: {
'keyup #signup-fullname': 'validateFullname'
},
validateFullName: function(e){
if($('#signup-fullname').val() == "mike"){
alert('You are just amazing!');
} else if($('#signup-fullname').val() == "tom"){
alert("mmm.. you fail...")
}
}
});
Isn't that a bit of an overkill can it be done with this or $(this)?
Thanks
Share Improve this question edited Jun 25, 2015 at 15:23 Evan Carroll 1 asked May 24, 2012 at 13:27 onlineracoononlineracoon 2,9705 gold badges48 silver badges66 bronze badges1 Answer
Reset to default 11In backbone.js "this" is bound to the view object.
If you need to access the target element you can still do so through the event.target or event.targetElement. Have a look at this question Backbone.js Event Binding