Skip to content

Instantly share code, notes, and snippets.

@mojowen
Created May 25, 2012 06:59
Show Gist options
  • Save mojowen/2786291 to your computer and use it in GitHub Desktop.
Save mojowen/2786291 to your computer and use it in GitHub Desktop.
Splitting a name field into first / last
jQuery(document).on(
'submit',
'#id',
function(e) {
var $this = jQuery(this),
name = jQuery('#name',$this).val().split(' '),
first_name = name.length > 1 ? name.shift() : name[0],
last_name = name.length > 0 ? name.join(' ') : '';
jQuery('#fname',$this).val(first_name).nextAll('#lname').val(last_name)
setTimeout(function() { jQuery('input',$this).not('[type=sumbit]').val('') }, 3)
}
);​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment