2

What is the best way to bind an event on multiple element . I wrote this code to do that is there is any good way to do that.

$('#GiftPurchase_recipient_first_name,#GiftPurchase_recipient_last_name').bind('blur', function() {
        gift_to  = $('#GiftPurchase_recipient_first_name').val()+" "+$('#GiftPurchase_recipient_last_name').val();
        $("#gift_to").val(gift_to);
    });

I have two input box with first name and last name, i wont to display there first anme and last name into a another element

Hasibur
  • 205
  • 1
  • 10

2 Answers2

0

Give the elements a class and add the event to the class.

$('.theclassnamethatalltheelementsshare').bind('blur', function() {
    gift_to  = $('#GiftPurchase_recipient_first_name').val()+""+$('#GiftPurchase_recipient_last_name').val();
    $("#gift_to").val(gift_to);
});
Henrik
  • 673
  • 8
  • 18
0

maybe this works

$(document).ready(function(){
    $('#GiftPurchase_recipient_first_name,#GiftPurchase_recipient_last_name').bind('blur',function(){
        var gift_to  = $('#GiftPurchase_recipient_first_name').val()+" "+$('#GiftPurchase_recipient_last_name').val();
        $("#gift_to").val(gift_to);
    });
});

Fiddle

Mirage
  • 162
  • 1
  • 7