Created
December 13, 2017 19:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example at codepen