Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect support for HTML5 <input form=""> attribute

How can I check whether a browser supports the HTML5 form attribute on input elements?

Following this question, I've tried the following:

var supportForm = function()
{
  var input = document.createElement("input");
  if ("form" in input)
  {
    input.setAttribute("form", "12345");

    if (input.form == "12345")
      return true;
  }

  return false;
}

... but that gives a false negative for FireFox (14, at least). Replacing input.form with input.getAttribute("form") gives a false positive for IE 9.

like image 845
Chowlett Avatar asked Oct 17 '12 13:10

Chowlett


People also ask

What does an HTML5 type attribute for an input element do?

The type attribute specifies the type of <input> element to display. If the type attribute is not specified, the default type is "text".

Can you list the new input type attributes in HTML5?

range: This input type allows the user to enter a numerical value within a specified range. search: This input type allows the user to enter a search string within the input field. tel: This input type allows the user to enter a telephone number. url: This input type allows the user to enter the URL.


1 Answers

For input elements there was a reference to the parent form before the HTML5 form reference feature and this causes this problem you mention.

I hope there is a more elegant way to check if it is supported but for now you could use the following (but it involves dealings with the DOM):

function testInputFormSupport() {
    var input = document.createElement('input'),
         form = document.createElement('form'),
         formId = 'test-input-form-reference-support';
    // Id of the remote form
    form.id = formId;
    // Add form and input to DOM
    document.childNodes[0].appendChild(form);
    document.childNodes[0].appendChild(input);
    // Add reference of form to input
    input.setAttribute('form', formId);
    // Check if reference exists
    var res = !(input.form == null);
    // Remove elements
    document.childNodes[0].removeChild(form);
    document.childNodes[0].removeChild(input);
    return res;
}
like image 175
Sev Avatar answered Sep 30 '22 16:09

Sev