Get An Input Element’s Type with jQuery

In a recent project, I’ve been working with large groups of input elements, and the course of action that’s taken depends on the type of element that raises the event.

Since so much of the project uses jQuery, I opted to write an extremely simple plugin that makes it easy to get an input’s type by calling a function on said element.

jQuery Get Input Type

jQuery Get Input Type

The plugin is extremely simple to use:

  1. Add the source to the head element of your HTML document
  2. Call $(this).getInputType() on the element in question, and the type will be returned in a lowercase string

For example (and straight from the demo):

$('.demo-input').each(function() {
    var sType = $(this).getInputType() );
});

From there, you can then take action on the element based on the type that is returned.

You can expect to see:

  • select
  • text
  • textarea
  • radio
  • checkbox
  • …and more

Obviously, this is really simple, but I’ve officially tagged 0.1.0 on GitHub. The repository includes the source code, the licensing, a simple demo, and the minified version of the code.

Using This in Projects

Obviously, the amount of code that’s code into this jQuery plugin is extremely small, so if you opt to include this in your project, I recommend using a tool such as CodeKit or another code minifier and dependency management utility to bundle this with your existing code.

Regardless, the project’s up on GitHub so feel free to open any issues or open any pull requests.

1 Reply to “Get An Input Element’s Type with jQuery”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.