I'm confused by the difference between "HTML attribute" and "property of the DOM element". From this post, I learned that getAttribute("class")
returns the former, while element.className
returns the latter. However, I could't understand the output of the following code:
var span = document.querySelector("#span");
console.log(span.className + ' ' + span.getAttribute("class"));
span.className = "bar";
console.log(span.className + ' ' + span.getAttribute("class"));
<span id="span" class="foo"><span>
I expect the result to be
foo foo
bar foo
because in my understanding the content of my HTML file hasn't been changed while javascript is run. Furthermore, when I replace the span.className = "bar";
line with span.setAttribute('class', 'bar');
, same result remains.
My question is: What's the difference and relationship between "HTML attribute" and "property of the DOM element"?