0

im fairly newb when it comes to writing pure javascript, but i want to learn and become better. i have a code below, what im trying to do in the if block doesnt matter so much, but what is tripping me up is using "this". below i want wrap to have classname if condition is true, but its not working. Can anyone please explain why is "this" not working in my code below?

var wrap = document.getElementById("wrap")

if(wrap.innerHTML.length === 0){
    this.className="empty"
}
ints
  • 65
  • 2
  • 12

1 Answers1

0

Try wrap.className = "empty".

this refers to the current "execution context" (which you can get a great explanation of in this previous answer). The fact that you're within an if statement doesn't change the execution context or anything... if you're just trying to add that className to the wrap element like you suggest, then apply the className to wrap not this.

Community
  • 1
  • 1
Shai
  • 6,314
  • 2
  • 15
  • 21