2

I am trying to add a class to a variable inside one of my if else statements in a jquery function. I am getting an error x.addclass is not a function. I have tried placing the variable in () and without. It makes no difference.

var ideal = document.getElementById( 'ideal_' + id )
var Search = document.getElementById('Search_' + id)

var parent = document.getElementById(+id)
if(ideal){
    parent.addClass('myclass')
}  else if (Search){
   parent.style.background='yellow'
}  else {
   parent.style.background='none'
} 
2
  • 1
    This is not jquery. try parent.classList.add('myclass') Commented Jan 17, 2021 at 5:20
  • .addClass() is a jQuery method to add classes to the selected element and .classList.add() is a Javascript way of adding classes. Commented Jan 17, 2021 at 6:16

1 Answer 1

2

.addClass() is a jQuery method, you can call that on a jQuery referenced element.

Try using classList.add():

parent.classList.add('myclass');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.