setAttribute throws exception for an attribute obtained from attributes list

node.attributes can give attributes that can't be set back on the element. Infact setAttribute will throw error when it encounters such an attribute.

Consider following html. Note, and extra " at the end of src attribute value.

<img id="img" src="img.png" />

Now, lets try getting the attributes of this node.


It will give three attributes, the last one being '"'. Lets try prefixing every attribute value with '_';

  var img = document.getElementById('img'),
    attributes = img.attributes;
  for (var i = 0; i < attributes.length; i++) {
    img.setAttribute(attributes[i].name, '_' + attributes[i].value)
... Loading comments