0

I'm quite new in javascript and this problem is haunting me down since the very begining. When I get DOM element event behaves properly, but while I try to change anything (like class) it gives me "undefined is not a function".

I don't understand why, because in the same time console.log gives out

<div class="nav-about menu" id="nav-about"></div>

HTML

<div class="nav-about menu" id="nav-about"></div>

JS

var link_one = document.getElementById("nav-about");

link_one.addEventListener("click", function(){
    console.log(this);
    this.addClass('special');
}, false);

I couldn't find answer to that anywhere. Yes jquery to addClass is included in head tag.

0

2 Answers 2

2

Because there is no addClass property on this, so it's undefined.

You could add the class on this.classList.

var link_one = document.getElementById("nav-about");

link_one.addEventListener("click", function(){
    this.classList.add('special');
}, false);
Sign up to request clarification or add additional context in comments.

Comments

1

try

$(this).addClass('special');

because you want to use jquery library, to use jquery library is like calling a function doSomething(this), but instead use $ as the function name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.