Here’s my CoffeeScript code:
$(document).ready ->
SearchView = Backbone.View.extend
tagName: "form"
className: "search"
events: {
"click label":"search"
}
search: ->
console.log("HERE")
searchView = new SearchView()
And here’s my HTML:
%form#search.search
%label
Search
%input
But clicking the form is not triggering any events. Any ideas why?
You misunderstand how
tagNameandclassNamework. When you set these:That just means that you’ll have
<form class="search"></form>in@el. Those settings don’t bind your view to what theform.searchselector matches and you never add@elto the DOM so your events don’t end up bound to anything at all.If you want to use
tagNameandclassNamethen you’ll want arenderto flesh out your form:And then you’d call
renderand addelto the DOM with something like this:Demo: http://jsfiddle.net/ambiguous/ubqqE/
If you wanted to bind to existing HTML then you could use
elinstead oftagNameandclassNamelike this:Demo: http://jsfiddle.net/ambiguous/FfDg6/