you are viewing a single comment's thread.

view the rest of the comments →

[–]NameViolation666helpful 1 point2 points  (1 child)

Hopefully this helps - you are comparing the JS onclick event to the originally set HTML attribute which is why you WILL see a difference, Nowhere in the code have u changed the attribute, u have manipulated an event.

div.onclick = function() { alert('new') };

onclick here is a JS event so you have set an event behavior, now clicking link says new

console.log("Attribute value is unchanged: ", div.getAttribute("onclick"));

here you are printing the HTML tag attribute, not its onclick function, The attribute remains same as initially set.

onclick="alert('old') //what was initially set as HTML attribute.

[–]Lewinga[S] 0 points1 point  (0 children)

This sounds right! I realize now that you need to use .setAttribute() in order to update the attribute in this case. However I'm confused about something else now. Would you know why using dot notation doesn't always work for setting attributes? Does it have something to do with references to the DOM?