A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > JavaScript > Test if Element Supports Attribute Submit one!

Test if Element Supports Attribute

Not all browsers support all attributes on all elements. There are a number of new attributes in HTML5, so the idea of testing to see what kind of browser environment you are in becomes every increasingly important.

function elementSupportsAttribute(element, attribute) {
  var test = document.createElement(element);
  if (attribute in test) {
    return true;
  } else {
    return false;
  }
};

Usage

if (elementSupportsAttribute("textarea", "placeholder") {

} else {
   // fallback
}

One Response

  1. aytac says:

    “(” –> “)”


    if (elementSupportsAttribute("textarea", "placeholder")) {

    } else {
    // fallback
    }

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.