1

I have two input fields and i need a function that do something when the value of #name or #email was changed. My start doesn't really work.

$('#name').keyup( function() {
// OR
$('#email').keyup( function() {

    // DO SOMETHING

});
});
3
  • 1
    this should work. what is wrong?? Commented Apr 22, 2014 at 7:47
  • 2
    api.jquery.com/multiple-selector Commented Apr 22, 2014 at 7:47
  • 1
    He set the closing brackets wrong... this won't work Commented Apr 22, 2014 at 7:48

3 Answers 3

6

You can use comma , to separate multiple selectors:

$('#name,#email').keyup( function() {
    // DO SOMETHING
});
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is nested, the second call to the $('#email') keyup event will be executed only if a keyup event on #name was triggered.

The code should be something like this:

$('#name,#email').keyup( function() {
    // DO SOMETHING
});

$('#name,#email').on('keyup', function() {
    // DO SOMETHING
});

Comments

0

You are closing the keyup function wrongly which is why not working:

$('#name').keyup( function() {
// OR
}); //closing here
$('#email').keyup( function() {

    // DO SOMETHING

});
//}); not here

A better solution for multiple selectors use like @Felix:

$('#name,#email').keyup( function() {
    // DO SOMETHING
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.