Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone.js - events, knowing what was clicked

In one of my backbone.js view classes, I have something like:

...  events: {   'click ul#perpage span' : 'perpage' },  perpage: function() {   // Access the text of the span that was clicked here   // Something like: alert($(element).text()) },  ... 

because my per page markup might have something like:

<ul id="perpage">   <li><span>5</span></li>   <li><span>10</span></li> </ul> 

So how exactly can I find information about the element that caused the event? Or in this instance, that was clicked?

like image 380
Matthew Avatar asked Apr 15 '11 18:04

Matthew


People also ask

How do I trigger an event in Backbone JS?

js trigger Event is used to invoke or callback the function for the given event or a space-delimited list of events. The subsequent arguments will be passed along to the event callbacks in order to trigger it. Parameter Values: event: It is used to bind an object with an event.

What is the only method available in the backbone JS history?

There is only method named "start" can be used to manipulate the Backbone. js history.

What are views in Backbone JS?

The Backbone. js Views specify how your data looks like. They represent model's data to the users. They can be used with any JavaScript template library.


1 Answers

Normally on an event bind, you would just use $(this), but I'm fairly sure Backbone views are set up so that this always refer to the view, so try this:

perpage: function(ev) {    alert($(ev.target).text()); } 

REALLY LATE EDIT: You probably want to use $(ev.currentTarget). See dicussion on pawlik's answer below

like image 81
Jamie Wong Avatar answered Oct 23 '22 03:10

Jamie Wong