Jungle Coder

The musings of a third culture coder and missionary kid

JS: DOM Attributes are lower-cased, and getAttribute is case insensitive

A short reminder for those of us writing JS without libraries:

DOM attributes are a little strange from JS. When you iterate over them using the attributes property, all the names come back completely lower cased. When you fetch them using getAttribute(name), the attribute name argument is completely case insensitive.

Short Code Example:

(Code shown below for anyone that just wants to see it)

<div id="my-id" data-ThisIsInfo="Hello, Internet">
  <script type="text/javascript" >
  var elem = document.getElementById("my-id");
for (var i = 0; i < elem.attributes.length; i++) {
  var attrib = elem.attributes[i];
  if (attrib.specified && attrib.name.indexOf("data-") === 0) {
    elem.innerText += attrib.name + "='" + attrib.value + "'";
    console.log(attrib);
  }
}

elem.innerText += elem.getAttribute("data-tHISiSiNFO");

  </script>
  Debug:
</div>
Previously: Life in PNG: Moving and moving and moving
Next: Late Night Programming

Comments


Email:*
Name:*
Reqired fields are marked with a '*'