0

For example, I have this jQuery function:

function example(){

  var trigger = $('.image');

  trigger.each(function(e){
    // here I want to change the background color of the :after pseudo-element
    $(this:after).css('background-color', 'red');
    // ^ the above obviously doesn't work
  });

}

How can I add, change or remove CSS attributes to a pseudo-element of $(this)?

Maarten Wolfsen
  • 1,625
  • 3
  • 19
  • 35
  • Jason, It is not a duplicate. The duplicate just asks to change the content of the pseudo-element. I want to change the CSS attributes – Maarten Wolfsen Oct 21 '16 at 12:56
  • 1
    Toggle a class instead and use relevant CSS rule to handle pseudo element, e.g `$(this).addClass('red');` and in CSS `.image.red:after {background-color: red;}` – A. Wolff Oct 21 '16 at 13:14

1 Answers1

0

You can't handle the ":after" pseudo-element because it doesn't exists in the DOM (same for :before). But you can create it using this:

$(element).after();

And then handle that with JS.

Hope it works for you!

Jimmy Adaro
  • 1,325
  • 15
  • 26