lisovaccaro
lisovaccaro

Reputation: 34006

If certain element is changed modify it?

Basically I have a form and when it's submited I add a class to blank fields to highlight them to be filled.

However when the user starts to fill an input or changes a "select" I want to remove this class.

This is what I'm trying, my problem is that I don't know how to select just the element that has been modified. How can I do this?

$("input, select").change(function(){
    $('input').removeClass('highlight'); // Only affect modified element
    });

Upvotes: 0

Views: 55

Answers (3)

Explosion Pills
Explosion Pills

Reputation: 191809

Simply use $(this). jQuery methods will use that argument as the event element by default. If you only want to affect input, then:

$(this).filter("input").removeClass(...)

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 360026

Use this. It's a very common jQuery idiom with all event (and many non-event) callbacks.

$("input, select").change(function(){
    $(this).removeClass('highlight'); // Only affect modified element
});

http://api.jquery.com/bind/#event-handlers

The handler parameter takes a callback function, as shown above. Within the handler, the keyword this refers to the DOM element to which the handler is bound. To make use of the element in jQuery, it can be passed to the normal $() function.

Upvotes: 1

elclanrs
elclanrs

Reputation: 94131

You could use $(this) I guess:

$("input, select").change(function(){
  $(this).removeClass('highlight'); // Only affect modified element
});

Upvotes: 1

Related Questions