Home Subscribe Comments Feed
   

Detecting Original HTML5 Input Types On Unsupported Browsers

April 25 2010   2 Comments

I’ve been doing some work recently on extending jQuery UI to style form elements using the Themeroller.  One thing that I wanted to implement was detection of the new HTML5 input types and choosing the correct widget to use based upon that.  This would provide progressive enhancement of the input element to support the HTML5 input type, even if that type of input isn’t supported on that browser.

By definition, if a browser can’t support the type of an input, it automatically falls back to a text input.  This is great for backwards compatibility, but it gave me a bit of a problem.  If I set type=”number” in my HTML, testing the type of the element in Javascript returns “text” when the number element isn’t supported.  Useful for determining if support is available, not so useful for determining what the original value is in the HTML.

In most browsers, you can find the original value using the attributes collection.  However, this doesn’t work in IE7 and earlier.  And, as everyone knows, we still have to support IE 6 & 7 for most web sites.  I found an alternative for IE, which is to test the outerHTML of the element using a regular expression.  IE will return this with the original attributes intact.

I’ve written a jQuery filter extension that allows you to test any element for the original input type:

// add HTML5 input type expression (still detects HTML5 input types on browsers that don't support them)
$.extend($.expr[':'], {
	inputtype: function(elem, i, type) {
		function getRawAttr() {
			// IE will return the original value in the outerHTML
			var match = /<input.*?\s+type=(\w+)(\s+|>).*?/i.exec(elem.outerHTML);

			if (match && match.length >= 2 && match[1] !== "text") {
				return match[1];
			}

			// for other browsers, test attributes collection (doesn't work in IE<7)
			var attrs = elem.attributes,
				i;

			for (i=0; i<attrs.length; i++) {
				if (attrs[i].nodeName === "type") {
					return attrs[i].nodeValue;
				}
			}

		}

		if (elem.tagName != "INPUT") return false;
		if (elem.type === "text") {
			// could be unsupported type fallback, so do further testing
			return getRawAttr() === type[3];
		} else {
			return elem.type === type[3];
		}
	}
});

To use this extension, just use the :inputtype(type) filter in your jQuery expression.  For example:


$('input:inputtype(number)).width(100);

if ($(elem).is(':inputtype(number)')) {
...
}

Update: Changed to only run the check if the tag is an input tag, in case you run the filter against non-input elements.

2 Responses to “Detecting Original HTML5 Input Types On Unsupported Browsers”

  1. Original HTML5 input type value – jQuery plugin » Rachael L. Moore says:
    December 16, 2010 at 7:48 pm

    [...] search landed me on Brant Burnett’s site, where he had written a jQuery plugin to add an :inputtype selector. Neat, but a few moments of experimentation led me to feel I wanted something slightly [...]

    Log in to Reply
  2. Get the original HTML5 input @type (jQuery plugin) « Rachael L. Moore says:
    September 12, 2011 at 3:57 pm

    [...] search landed me on Brant Burnett’s site, where he had written a jQuery plugin to add an :inputtype selector. Thank you very much, Brant!  I wanted something slightly different, though, so I modified [...]

    Log in to Reply

Leave a Reply

Click here to cancel reply.

You must be logged in to post a comment.

« Simplify CSS and Javascript Compression In Visual Studio
jQuery Smooth Animations In Firefox 4 »
 
     
Copyright © 2009 All Rights Reserved. Powered by WordPress 2.7 Subscribe to RSS
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.