Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Do I need both keyUp and change events?

I have a page with 2 forms; each form has an input text field;

The value found in the input text of the 1st form must be the same with the value found in the input text of the 2nd form;

When the value changes, the other input text field also modifies its value;

Should I use both onkeyup and onchange events?

If not, which one is the right one to use?

I am asking this because it seams that I have 2 post requests, and I think that this is why.

   $('input.searchbox').keyup( function(){        $('input.txtfieldsearch').val($('input.searchbox').val());        listViewUpdate();        $('input.txtfieldsearch').trigger("keyup");    });                      $('input.txtfieldsearch').keyup( function(){        $('input.searchbox').val($('input.txtfieldsearch').val());        listViewUpdate();     });                     $('input.searchbox').change( function(){        $('input.txtfieldsearch').val($('input.searchbox').val());        listViewUpdate();        $('input.txtfieldsearch').trigger("change");    });                      $('input.txtfieldsearch').change( function(){        $('input.searchbox').val($('input.txtfieldsearch').val());        listViewUpdate();     }); 
like image 658
Ionut Flavius Pogacian Avatar asked Apr 18 '13 11:04

Ionut Flavius Pogacian


People also ask

What is the difference between Keyup and Keydown?

When user presses a key or combination of different keys, keydown , keypress and keyup are triggered in that order: The keydown event is triggered first when user presses a key. The keyup event is triggered last when user releases a key.

What is the use of Keyup?

The keyup event occurs when a keyboard key is released. The keyup() method triggers the keyup event, or attaches a function to run when a keyup event occurs.

What is Keyup jQuery?

The keyup() is an inbuilt method in jQuery which is used to trigger the keyup event whenever User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Syntax: $(selector).keyup(function) Here selector is the selected element.

What is Keyup event?

The keyup event is fired when a key is released. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .


1 Answers

You can use $.on() function and attach multiples handlers.

$("#element").on('keyup change', function (){    // Your stuff... }); 
like image 147
rigobcastro Avatar answered Sep 21 '22 13:09

rigobcastro