It doesn't help that you are parsing it as XML, yet it isn't valid XML (no closing tag). Try this:
new XMLSerializer().serializeToString(a)
You get this parser error:
<img src="/hello" alt="Promised">
<parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
<h3>This page contains the following errors:</h3>
<div style="font-family:monospace;font-size:12px">error on line 1 at column 34: Extra content at the end of the document
</div>
<h3>Below is a rendering of the page up to the first error.</h3>
</parsererror>
</img>
If you fix the XML (closing tag) and repeat, you get:
var a = new DOMParser().parseFromString('<img src="/hello" alt="Promised"></img>', "text/xml");
new XMLSerializer().serializeToString(a)
Results in:
<img src="/hello" alt="Promised"/>
If you parse it as HTML instead:
var a = new DOMParser().parseFromString('<img src="/hello" alt="Promised">', "text/html");
You'll get this instead:
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<img src="/hello" alt="Promised" />
</body>
</html>
To loop over the element's attributes, you need to fix your loop:
var elem = a.getElementsByTagName('img')[0];
for (var i = 0; i < elem.attributes.length; i++) {
var attrib = elem.attributes[i];
if (attrib.specified) {
console.log('b is: ' + attrib.name + '. Value is: ' + attrib.value);
}
}