gringogordo
gringogordo

Reputation: 2130

How to bind an event to all input fields in jQuery

I'm trying to bind all input fields on a 'form page' to the blur event in jquery. If I put a breakpoint in the each it gets paused so I think the code is being called on page load properly and the loop is working okay. I think the syntax is correct for binding the event as well ? I am not getting the alert when I do anything (leave!) any of the fields in my form though so I think something about this is wrong ? Can anyone point it out to me ?

//for each item in the form (:input "selects all input, textarea, select and button elements.") 
    $(' :input').each(function () {
        $('this').bind('blur', function(){
            alert ("nasty, shoudl be a log!");
            myfunction();
        })
    })

Upvotes: 0

Views: 439

Answers (2)

ACostu
ACostu

Reputation: 1

$(":input").bind('blur',() => yourfunction());

Just add the event call into your jQuery selector.

Upvotes: 0

user8246956
user8246956

Reputation:

You should use:

    $(this).bind('blur', function(){

without the quotes.

Upvotes: 1

Related Questions