-1

so hello i am trying to add class that for element that passes all selectors this is my current code

document.querySelectorAll('.inner p .align_center').parentNode.classList.add('center_text')

which uhh should work, i am trying to translate this jquery code

$(".inner p .align_center").parent().addClass('center_text')

but i get .parentNode is undefined in the console why? i selected child of p with class of .alignt-center and i want to add class to that p element, or is there easier way of doing the same thing? what am i doing wrong

Olian04
  • 6,480
  • 2
  • 27
  • 54
Picarica
  • 23
  • 4
  • yes, very much thanks, didnt know it sorts it in array, didnt had to downvote it tho but thanks in t he end – Picarica Nov 13 '20 at 22:38
  • Np. Yes downvote is mine, but you could have been wrong, never assume that around here. And I will explain it. This question is answered at least 5 times a day here on SO. And if you have looked at documentation yourself of usage, you would have found the answer easily. – ikiK Nov 13 '20 at 22:48
  • btw if you excpet just one element just use `document.querySelector` – ikiK Nov 13 '20 at 22:50

1 Answers1

0
var a = document.querySelectorAll('.inner p .align_center');
for(var i = 0; i < a.length; i++) {
    a[i].parentNode.classList.add('center_text');
}

just had to treat querySelectorAll as a array instead of single element

Picarica
  • 23
  • 4