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
The plugin is extremely simple to use:
- Add the source to the `head` element of your HTML document
- 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.
Leave a Reply
You must be logged in to post a comment.