Custom events and event pooling in jQuery - What's the point?

Posted by Nick Lowman on Stack Overflow See other posts from Stack Overflow or by Nick Lowman
Published on 2010-06-07T10:54:37Z Indexed on 2010/06/07 11:12 UTC
Read the original article Hit count: 193

Filed under:
|

I've been reading about custom events in jQuery and why they should be used but I'm still clearly missing the point. There is a very good article I read here that has the following code example;

function UpdateOutput() {
    var name = $('#txtName').val();
    var address = $('#txtAddress').val();
    var city = $('#txtCity').val();

    $('#output').html(name + ' ' + address + ' ' + city);
}

$(document).bind('NAME_CHANGE ADDRESS_CHANGE CITY_CHANGE', function() {
    UpdateOutput();
});

$('#txtAddress').keyup(function() {
    $(document).trigger('ADDRESS_CHANGE');
});
$('#txtCity').keyup(function() {
    $(document).trigger('CITY_CHANGE');
});

Can someone tell me why I just don't call the UpdateOutput() function directly? It would still work exactly the same way, i.e.

$('#txtAddress').keyup(function() {
    UpdateOutput()
});
$('#txtCity').keyup(function() {
    UpdateOutput()
});

Many thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery