Attach event listener to elements of a given class
So this may be easy but I can't get it work.
I have 3 text inputs of filter
class. What I want is bind a function search()
to onkeypress
event. I know I can write it manually to every input like this onclick="search()"
but I don't want to use inline things, just prototype functions.
My current code is:
<input type="text" class="filter" id="id"/>
<input type="text" class="filter" id="title"/>
<input type="te开发者_如何转开发xt" class="filter" id="quant"/>
and
Event.observe('.filter', 'keypress', function(){ // <<< this don't work :(
alert('yey!');
});
I'm not too familiar with prototypejs, but I believe you can do this:
Example: http://jsfiddle.net/patrick_dw/yAj3A/
$$('.filter').invoke('observe','keypress', function(){
alert('yey!');
});
EDIT: Or if you wanted to use a named function, you can do that too.
Example: http://jsfiddle.net/patrick_dw/yAj3A/1/
function search(){
alert('yey!');
}
$$('.filter').invoke('observe','keypress', search);
精彩评论