instead of example:
$(".myfield").blur(function() {
// validate
})
$(".myfield").keyup(function() {
// validate
})
is there a way to combine these two?
The jQuery . on() can attach multiple events on an element. In the below code I have attached 2 events to the p element. So when the element is clicked or mouse leaves this element, you will get alert box displayed.
To achieve this you will have to bind a jQuery click event with the <div> element and then define an action against the click event.
You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements.
The on() is an inbuilt method in jQuery which is used to attach one or more event handlers for the selected elements and child elements in the DOM tree.
Yes
$(".myfield").bind('blur keyup', function(){
// validate
});
Reference: .bind()
In case you want to validate each for itself...
$('.myfield').live('blur keyup', function(event) {
if (event.type == 'blur') {
// validate on blur
}
if (event.type == 'keyup') {
// validate on keyup
}
});
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