0

I am using the Wordpress theme Avada and I want to append a class to a pagination <div>. Currently it is like:

<div class="pagination clearfix"> and I would like to dynamically be able to add a class to this div like so: <div class="wp-pagenavi pagination clearfix">

Is there a way of doing this? I have searched the theme and cannot find where this <div> is so that's the reason for wanting to change it hopefully somehow dynamically. I hope it makes sense. Thanks for any help in advance,

Peter

Difster
  • 3,240
  • 2
  • 19
  • 32
Peter
  • 11
  • 1
  • Possible duplicate of [How do I add a class to a given element?](https://stackoverflow.com/questions/507138/how-do-i-add-a-class-to-a-given-element) – Ali Adlavaran Aug 05 '17 at 19:01

1 Answers1

1

you can do it using javascript, idk about wordpress and if u can get access to the code, but with javascript you can do the following:

document.addEventListener('DOMContentLoaded',function(){
document.querySelector('.pagination').classList.add('wp-pagenavi');
console.log(document.querySelector('div'));
});
<div class="pagination clearfix">

DOMContentLoaded

DOMContentLoaded wait until the Dom is ready and after execute the srcipt

Risa__B
  • 405
  • 4
  • 8
  • Hi, I see that that script works here very well. However on my page in the console it give me this error: Uncaught TypeError: Cannot read property 'classList' of null – Peter Aug 05 '17 at 19:38
  • do you have another divs or elements with the same class? – Risa__B Aug 05 '17 at 19:51
  • No. This is how it is when I view the source page: – Peter Aug 05 '17 at 20:01
  • i've edited the code, try again, and if that not work i still have a third option – Risa__B Aug 05 '17 at 20:19
  • No go. Same error at this line: document.querySelector('.pagination').classList.add('wp-pagenavi'); – Peter Aug 05 '17 at 20:24
  • are you executing your script from console? or are you adding the script to your wp page? – Risa__B Aug 05 '17 at 20:32
  • I tried using this: var elements = document.getElementsByClassName("pagination clearfix"); elements[0].className += " wp-pagenavi"; and got an error also : Uncaught TypeError: Cannot read property 'className' of undefined – Peter Aug 05 '17 at 20:34
  • I am adding the script in the header of my wp page – Peter Aug 05 '17 at 20:34
  • not, you need to add the script in the bottom of your page, before close body tag, but if you want have the script in the header i will show you the 3rd option – Risa__B Aug 05 '17 at 20:37
  • Ok, one second and will add before closing body tag – Peter Aug 05 '17 at 20:38
  • not problem brother, i've edited the code, so you can too put the script in your head tag – Risa__B Aug 05 '17 at 20:43