Javascript: function with "this" undefined -
I'm new to JavaScript and I'm trying to write a function that changes the color of some text, though "This" keeps returning as an undefined, and the text color is not changing. Here's the code, I appreciate some help.
& lt; H1 & gt; & Lt; Ul & gt; & Lt; Gt; & lt; div class = "menu" id = "menu" onclick = "test1 ()" & lt;! - this is where the problem is -> MENU
You must pass reference to the DOM element in the function
& lt ; Div class = "menu" id = "menu" onclick = "test1 (this)" & gt; / div> In the above html, it refers to the current DOM element
and then references and modifies the DOM inside the function
function test1 (obj) {obj.style.color = '# 9DC8BA';} Side note: You have the same name for div and id for div, which is not a good practice Always remember that the class elements Matches a group and the ID matches the unique elements.
Comments
Post a Comment