Skip to content

Instantly share code, notes, and snippets.

@williamdodson
Created December 7, 2015 15:56
Show Gist options
  • Save williamdodson/eadfce84482b30414567 to your computer and use it in GitHub Desktop.
Save williamdodson/eadfce84482b30414567 to your computer and use it in GitHub Desktop.
jQuery plugin to return a given input type as a string (e.g. "textarea").
/**
* Function getInputType
*
* @return String name for the type of input (i.e. select or textarea)
* @example <code>var inputType = $(input).getInputType(); // returns a string representation of the given input</code>
* @author William Dodson <william.dodson@gmail.com>
* @version 1.0.0
*/
(function ($) {
'use strict';
$.fn.getInputType = function () {
var inputType;
// return early if we get passed an undefined field name
if(typeof this[0] === 'undefined') {
return;
}
inputType = this[0].tagName.toString().toLowerCase() === 'input' ?
$(this[0]).prop('type').toLowerCase() : this[0].tagName.toLowerCase();
return inputType;
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment