For example, I have a input text and a button
I want two events, button.on click and input.on KeyEnterPress to bind to one same function
How can I do that?
$('#search.onClick OR $searchText.onKeyEnterPress')
In short: . bind() will only apply to the items you currently have selected in your jQuery object. . live() will apply to all current matching elements, as well as any you might add in the future.
mouseMoveHandler, false); JQuery's bind allows multiple events, like so: $(window). bind('mousemove touchmove', function(e) { //do something; });
bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.
Create a function to call:
function myfunc(event){
// some code
}
You can do
$('#search').add($searchText).on('click keyup', myfunc);
OR if you ONLY want those on one (not both) do:
$('#search').on('click', myfunc);
$($searchText).on('keyup', myfunc);
NOTE Not clear if that last is a jQuery object but if so:
$searchText.on('keyup', myfunc);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With