add class to $(el).contents() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

add class to $(el).contents()

hello, I've this: var headers = $('h1, h2, h3').contents(); for(var i=0; i < Object.keys(headers).length-2; i++) { if(headers[i].data) { // security var d = headers[i].data; var headerClass = d.replace(' ','-').toLowerCase()+'-'+i; // here is my question : how to set the class headerClass to my headers[i] ? } }

7th May 2019, 8:25 PM
NoxFly
NoxFly - avatar
3 Answers
+ 5
header[i].addClass(headerClass);
8th May 2019, 6:02 AM
Toni Isotalo
Toni Isotalo - avatar
+ 2
Toni Isotalo `headers[i].addClass is not a function` I already tried ^^ And for headers[i].parentElement.classList.add = headerClass; I dont have error but any header have class If I remove the .parentElement I've same error as above
9th May 2019, 12:34 PM
NoxFly
NoxFly - avatar
+ 1
then it is $(headers[i]).addClass(headerClass);
9th May 2019, 12:39 PM
Toni Isotalo
Toni Isotalo - avatar