Skip to content

Instantly share code, notes, and snippets.

@thomhines
Created December 13, 2017 19:58
Show Gist options
  • Save thomhines/22903d0ccddfbf16ec0aa33bb20679cb to your computer and use it in GitHub Desktop.
Save thomhines/22903d0ccddfbf16ec0aa33bb20679cb to your computer and use it in GitHub Desktop.
Better jQuery val() function - Returns value of all types of inputs, including radio buttons and checkboxes
jQuery.fn.getValue = function() {
if(this.is(':checkbox') && this.is(':checked')) return true;
else if(this.is(':checkbox') && !this.is(':checked')) return false;
else if(this.is(':radio') && $('input[name="'+this.attr('name')+'"]').filter(':checked').val()) return $('input[name="'+this.attr('name')+'"]').filter(':checked').val();
else if(this.is(':radio') && !$('input[name="'+this.attr('name')+'"]').filter(':checked').val()) return false;
else if(this.val()) return this.val();
return false;
}
@thomhines
Copy link
Author

Example at codepen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment