blackjid
5/7/2012 - 4:41 PM

A clean way to get data from a form element using the submit event

A clean way to get data from a form element using the submit event

//Requires underscore, jquery

$(document).on('submit', 'selector.to.the.form.element', function(e){
    
    // The the data from the form
    var formData = _.reduce($(e.currentTarget).serializeArray(), function(memo, val){
        memo[val.name] = val.value;
        return memo;
    },{});
    
    // HERE YOU CAN USE THE FORM DATA ...

    return false;
});