Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 Type Detection and Plugin Initialization
    primarykey
    data
    text
    <p><strong>PART A:</strong></p> <p>I know there are a lot of things out there that tell you if a <strong>browser</strong> supports a certain HTML5 attribute, for example <a href="http://diveintohtml5.info/detect.html" rel="nofollow noreferrer">http://diveintohtml5.info/detect.html</a>, but they don't tell you how to acquire the type from individual elements and use that info to init your plugins. </p> <p>So I tried:</p> <pre><code>alert($("input:date")); //returns "[object Object]" alert($("input[type='date']")); //returns "[object Object]" alert($("input").attr("type")); //returns "text" ... which is a lie. it should have been "date" </code></pre> <p>None those worked.</p> <p>I eventually came up with this (that does work):</p> <pre><code>var inputAttr = $('&lt;div&gt;').append($(this).clone()).remove().html().toLowerCase(); alert(inputAttr); // returns "&lt;input min="-365" max="365" type="date"&gt;" </code></pre> <p><em>Thanks: <a href="http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html" rel="nofollow noreferrer">http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html</a></em></p> <p>So my first question: 1. Why can I not read the "type" attribute in browsers that don't support html5? You can make up any other attribute and bogus value and read it. 2. Why does my solution work? Why does it matter if its in the DOM or not?</p> <p><strong>PART B:</strong></p> <p>Below is a basic example of what I am using the detector for: </p> <pre><code> &lt;script type="text/javascript" &gt; $(function () { var tM = document.createElement("input"); tM.setAttribute("type", "date"); if (tM.type == "text") { alert("No date type support on a browser level. Start adding date, week, month, and time fallbacks"); // Thanks: http://diveintohtml5.ep.io/detect.html $("input").each(function () { // If we read the type attribute directly from the DOM (some browsers) will return unknown attributes as text (like the first detection). Making a clone allows me to read the input as a clone in a variable. I don't know why. var inputAttr = $('&lt;div&gt;').append($(this).clone()).remove().html().toLowerCase(); alert(inputAttr); if ( inputAttr.indexOf( "month" ) !== -1 ) { //get HTML5 attributes from element var tMmindate = $(this).attr('min'); var tMmaxdate = $(this).attr('max'); //add datepicker with attributes support and no animation (so we can use -ms-filter gradients for ie) $(this).datepick({ renderer: $.datepick.weekOfYearRenderer, onShow: $.datepick.monthOnly, minDate: tMmindate, maxDate: tMmaxdate, dateFormat: 'yyyy-mm', showAnim: ''}); } else { $(this).css('border', '5px solid red'); // test for more input types and apply init to them } }); } }); &lt;/script&gt; </code></pre> <p>Live example: <a href="http://joelcrawfordsmith.com/sandbox/html5-type-detection.html" rel="nofollow noreferrer">http://joelcrawfordsmith.com/sandbox/html5-type-detection.html</a></p> <p>And a favor/question: Can anyone help me cut some fat in my HTML5 input type fixer? </p> <p>I have the functionality down (adds fallbacks to IE6-IE8, and FF with out adding classes to init off of)</p> <p>Are there are more efficient methods for iterating over the DOM for mystery input types? And should I be using an If Else, or a function, or a case in my example?</p> <p>Thanks All,</p> <p>Joel</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload