Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery

access the css “:after” selector with jquery
add before css using jquery
jquery ::before css
jquery ::before selector
jquery ::after selector
set pseudo-element properties with javascript
jquery check if pseudo element exists
how to select after jquery

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, my stylesheet has the following rule:

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

How can I 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:

In HTML:

<span>foo</span>

In jQuery:

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

In CSS:

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

If you want to prevent the 'other text' from showing up, you could combine this with seucolega's solution like this:

In HTML:

<span>foo</span>

In jQuery:

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

In CSS:

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

Changing width property of a :before css selector using JQuery , Pseudo-elements are not part of the DOM, so they can't be manipulated using jQuery You can not select an element via pseudo-selector, but you can add a new manipulating CSS pseudo-elements such as ::before and ::after using jQuery. 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, my stylesheet has the following rule: .

You'd think this would be a simple question to answer, with everything else that jQuery can do. Unfortunately, the problem comes down to a technical issue: css :after and :before rules aren't part of the DOM, and therefore can't be altered using jQuery's DOM methods.

There are ways to manipulate these elements using JavaScript and/or CSS workarounds; which one you use depends on your exact requirements.


I'm going to start with what's widely considered the "best" approach:

1) 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:

p:before {
    content: "foo";
}
p.special:before {
    content: "bar";
}

Then you can easily add or remove this class using jQuery (or vanilla JavaScript):

$('p').on('click', function() {
    $(this).toggleClass('special');
});

    $('p').on('click', function() {
      $(this).toggleClass('special');
    });
p:before {
  content: "foo";
  color: red;
  cursor: pointer;
}
p.special:before {
  content: "bar";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

Applying styles to :before and :after elements, How to select and manipulate CSS pseudo-elements such as ::before and ::after using jQuery ? For example, the stylesheet has the following rule: .span::after{  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, my stylesheet has the following rule:

How can select ::before with jQuery - JavaScript, 2) Add a style for .foo:before and use the css rules provided. 1 javascript - Selecting and manipulating CSS pseudo-elements such as ::before  [ Solved-10 Answers ] CSS - Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery Is there any way to select/manipulate

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.

EXAMPLE

In jQuery:

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

In CSS:

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

Before Selector, ::before is a pseudo-selector, not a dom element. So you can't select it. I haven't tried these Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery. javascript, jquery, css, jquery-selectors. This actually answers the question because there is a case (my case) where not all of the :after styles can be pre-created. Most are pre-written in css, but the one that needs to be added via js is a url to a background image.

We can also rely on custom properties (aka CSS variables) in order to manipulate pseudo-element. We can read in the specification that:

Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc.

Considering this, the idea is to define the custom property within the element and the pseudo-element will simply inherit it; thus we can easily modify it.

1) Using inline style:

.box:before {
  content:var(--content,"I am a before element");
  color:var(--color, red);
  font-size:25px;
}
<div class="box"></div>
<div class="box" style="--color:blue;--content:'I am a blue element'"></div>
<div class="box" style="--color:black"></div>
<div class="box" style="--color:#f0f;--content:'another element'"></div>

How to manipulate CSS pseudo-elements ::before and ::after using , The CSS ::before selector can be used to insert anything before the content an and manipulating CSS pseudo-elements such as ::before and ::after using jQuery​. What are Pseudo-Elements? A CSS pseudo-element is used to style specified parts of an element. For example, it can be used to: Style the first letter, or line, of an element; Insert content before, or after, the content of an element

Pseudo-classes and pseudo-elements, To manipulate CSS pseudo elements using the hover() function. src="https://​ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  jQuery Web Development Front End Technology To manipulate CSS pseudo elements using the hover() function. You can try to run the following code to learn how to manipulate CSS pseudo-elements −

Setting/Modifying :before and :after in javascript, 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  Although they are rendered by browsers through CSS as if they were like other real DOM elements, pseudo-elements themselves are not part of the DOM, because pseudo-elements, as the name implies, are not real elements, and therefore you can't select and manipulate them directly with jQuery (or any JavaScript APIs for that matter, not even the

For example, if you wanted to select the first line of a paragraph you could wrap Try editing the previous live example so it uses the following CSS. You could use these to insert a string of text, such as in the live example below. The use of the ::before and ::after pseudo-elements along with the content  jquery get pseudo element jquery pseudo element click jquery change pseudo element css set pseudo-element properties with javascript jquery change before content javascript get pseudo element access the css after selector with jquery jquery pseudo selectorjquery ::after click event clickable pseudo elements jquery before click event before

Comments
  • I made something that should work for you: gist.github.com/yckart/5563717
  • I hate such comments on stackoverflow that I'm going to write (where commenter asks why don't you do it some complete opposite way?), but: at some point one should realize code-design issue and replace that pseudo element with a span or something. I guess you know what I'm pointing at.
  • Accepted answer is excellent. Though if you are trying to achieve any of the following points, the answer wont work: 1. change style of :before by JS. 2. change content of :before by JS Please see my answer for that if you need it.
  • See github.com/guest271314/pseudo/blob/master/pseudo.js
  • @Learner Now there is an answer tfor all of these: stackoverflow.com/a/49618941/8620333
  • Do you have a link in the spec for using that attr function against the content property? I'm surprised I've never heard of this...
  • +1 for attr(), too bad i wasn't able to use it with other properties than content. Demo
  • That's because no browser in existence has implemented attr() beyond CSS2 yet, whereas in CSS2 itself attr() has only been defined for the content property.
  • Updated link for Attribute References: w3.org/TR/css3-values/#attr-notation
  • @Johannes If a browser supports :before, it supports attr() in CSS