Joanhard
Joanhard

Reputation: 89

Ask jQuery keyup event

how can id="e3" can be filled if id="e2" filled by id="e1"?

checking live code here, http://jsfiddle.net/eHEtV/

jQuery(document).ready(function(){

        jQuery('#e1').on('keyup', function(){
            var text = jQuery(this).val();
            jQuery(this).next().val(text);
        });

        jQuery('#e2').on('keyup', function(){
            var text = jQuery(this).val();
            jQuery(this).next().val(text);
        });    

});



<input id="e1" value="" />
<input id="e2" value="" />
<input id="e3" value="" />

Upvotes: 1

Views: 117

Answers (3)

thefrontender
thefrontender

Reputation: 1842

You could simplify your code since the event handler is identical for both inputs:

jQuery(document).ready(function(){

    jQuery('#e1, #e2').on('keyup', function(){
        var $el = jQuery(this);
        $el.next().val($el.val()).trigger('keyup');
    });   

});

Updated JSFiddle :http://jsfiddle.net/thefrontender/eHEtV/3/

Upvotes: 0

nix
nix

Reputation: 3302

You can add .trigger('keyup') to trigger the event on #e2 like this:

jQuery('#e1').on('keyup', function(){
    var text = jQuery(this).val();
    jQuery(this).next().val(text).trigger('keyup');
});

Upvotes: 2

PSL
PSL

Reputation: 123739

Using .trigger(eventname) you can achieve that.

Here i am triggering keyup of #e2 in #e1 keyup. #e2 keyup copies data to #e3.

 jQuery('#e1').on('keyup', function(){
        var text = jQuery(this).val();
        jQuery(this).next().val(text).trigger('keyup');
    });

Test Demo

Upvotes: 1

Related Questions