Is there any way to select/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery?

For example, the stylesheet has the following rule:

Css Code
.span::after{ content:'foo' }

How to change ‘foo’ to ‘bar’ using jQuery?

You could also pass the content to the pseudo element with a data attribute and then use jQuery to manipulate that:

 HTML:

Html Code
<span>foo</span>

 jQuery:

Java Code
$('span').hover(function(){
$(this).attr('data-content','bar');
});

 CSS:

Css Code
span:after {
content: attr(data-content) ' any other text you may want';
}

To prevent the ‘other text’ from showing up, we could combine this like this:

HTML:

Html Code
<span>foo</span>

 jQuery:

Java Code
$('span').hover(function(){
$(this).addClass('change').attr('data-content','bar');
});

 CSS:

Css Code
span.change:after {
content: attr(data-content) ' any other text you may want';
}

Add/remove a predetermined class :

In this approach, you’ve already created a class in your CSS with a different :after or :before style. Place this “new” class later in your stylesheet to make sure it overrides:

Css Code
p:before {
content: "foo";
}
p.special:before {
content: "bar";
}
Then you can easily add or remove this class using jQuery :

$('p').on('click', function() {
$(this).toggleClass('special');
});
[ad type=”banner”]

You can’t select pseudo elements in jQuery because they are not part of DOM.
But you can add an specific class to the father element and control its pseudo elements in CSS.

 jQuery:

Html Code
<script type="text/javascript">
$('span').addClass('change');
</script>

 CSS:

Css Code
span.change:after { content: 'bar' }

you could also do:

Css Code
$('head').append("<style>.span::after{ content:'bar' }</style>");

Here is the way to access :after and :before style properties, defined in css:

Css Code
// Get the color value of .element:before
var color = window.getComputedStyle(
document.querySelector('.element'), ':before'
).getPropertyValue('color');

// Get the content value of .element:before
var content = window.getComputedStyle(
document.querySelector('.element'), ':before'
).getPropertyValue('content');
[ad type=”banner”]

  • IF you want to manipulate the ::before or ::after pseudo elements entirely through CSS, you could do it using JavaScript as shown below.
Java Code
jQuery('head').append('<style id="mystyle" type="text/css"> /* your styles here */ </style>');
  • Notice how the <style> element has an ID, which you can use to remove it and append to it again if your style changes dynamically.
  • This way, your element has its style exactly how you want it through CSS, with the help of JS.

  • Add a rule to the document with the new content and reference it with a class. depending on what is needed .the class might need an unique id for each value in content.
Css Code
$("<style type='text/css'>span.id-after:after{content:bar;}</style>").appendTo($("head"));
$('span').addClass('id-after');

  • Why adding classes or attributes when you can just append a style to head
Css Code
$('head').append('<style>.span:after{ content:'changed 			content' }</style>')

  • Write separate classes attached with psuedo element for each style and then using JavaScript or jQuery toggle between these classes as shown below.
Css Code
.green::before {
content: 'green';
color: green;
}
$('p').removeClass('red').addClass('green');

  • Inject new styles to the existing document stylesheet directly either via JavaScript and the webpage will automatically reflect the new css.
Css Code
document.styleSheets[0].addRule('.red::before','color: green');
document.styleSheets[0].insertRule('.red::before { color: green }', 0);
[ad type=”banner”]

Categorized in: