Modify CSS of the :after pseudo-element using jQuery

Code Poet

I use the :after pseudo-element to display a decoration (triangle) after a block (<li> in my case). The idea is to distinguish the currently selected li from others.

Fiddle here

The html follows:

<ul>
    <li class="active" style="background-color: hsl(108, 60%, 50%)">One</li>
    <li style="background-color: hsl(36, 60%, 50%)">Two</li>
    <li style="background-color: hsl(252, 60%, 50%)">Three<li>
</ul>

and the css:

ul li {
    width: 300px;
    height: 30px;
    border: 1px dashed;
    position: relative;
}

li.active::after {
    content: " 0020";
    display: block;
    font-size: 0px;
    position: absolute;
    left:100%;
    top: 0%;
    width: 0px;
    height: 0px;
    background: transparent;
    border: 17px solid transparent;
    border-left-color: #FF3900;
}

I want to change the border-left-color style attribute of li.active::after pseudo element to match the background-color of the <li> element with class=active.

I came up with the following jquery:

$("ul li").click(function() {
    $("ul li").removeClass("active");
    $(this).addClass("active");
    $("li.active::after").css('border-left-color', $(this).css('background-color'));
});

This doesn't work as expected. Any help is appreciated.

DMTintner

you can't select pseudo elements such as :before and :after with jquery. But in your case you can do a workaround to use the parent li's style on the :after and thus match the border-color property

codepen

CSS updated:

ul li {
    width: 300px;
    height: 30px;
    border: 1px dashed;
    position: relative;
}
li:first-of-type.active {
  border-left-color: green; // your exact colors here
}
li:nth-of-type(2).active {
  border-left-color: orange;
}
li:last-of-type.active {
  border-left-color: purple;
}
li.active::after {
    content: " 0020";
    display: block;
    font-size: 0px;
    position: absolute;
    left:100%;
    top: 0%;
    width: 0px;
    height: 0px;
    background: transparent;
    border: 17px solid transparent;
    border-left-color: inherit;
}

and then just remove the line of js that includes the :after selector. not needed anymore

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modify CSS of the :after pseudo-element using jQuery

From Dev

Modify the css of a pseudo element using JQuery

From Dev

Using the :after CSS pseudo-element without inserting content

From Dev

Update CSS of Pseudo Element with jQuery

From Java

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

From Dev

LESS CSS selector combining not and :after pseudo element

From Dev

CSS ::after pseudo element clear not working

From Dev

Pseudo element in after starting from center - CSS

From Dev

JQuery select pseudo-element :after

From Dev

How to dynamically alter the CSS of a pseudo element with JQuery?

From Dev

Image overlay using :after pseudo-element

From Dev

Select the first CSS element using a pseudo class

From Dev

CSS, using display:table with before pseudo element

From Dev

Insert &times; using CSS pseudo element

From Dev

Creating a shape using a :before pseudo element in CSS

From Dev

CSS - Using a pseudo-element for an image mask

From Dev

Cannot rotate :after pseudo-element using CSS in IE8

From Dev

Using :after pseudo element after anchor element - browser differences

From Dev

Toggle a pseudo-element using jQuery

From Dev

Toggle a pseudo-element using jQuery

From Dev

CSS: <a> tags disabled after using pseudo elements

From Dev

How do I modify a css element with Jquery after appended with Jquery to a div?

From Dev

How do I modify a css element with Jquery after appended with Jquery to a div?

From Dev

Using jQuery to show/hide an :after pseudo class

From Dev

Benefit of using jQuery to modify CSS

From Dev

Modify the html by using jquery css

From Dev

modify pseudo select :after in javascript

From Dev

CSS ::before ::after pseudo-element of class not working

From Dev

CSS centering content add by pseudo-element after

Related Related

  1. 1

    Modify CSS of the :after pseudo-element using jQuery

  2. 2

    Modify the css of a pseudo element using JQuery

  3. 3

    Using the :after CSS pseudo-element without inserting content

  4. 4

    Update CSS of Pseudo Element with jQuery

  5. 5

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

  6. 6

    LESS CSS selector combining not and :after pseudo element

  7. 7

    CSS ::after pseudo element clear not working

  8. 8

    Pseudo element in after starting from center - CSS

  9. 9

    JQuery select pseudo-element :after

  10. 10

    How to dynamically alter the CSS of a pseudo element with JQuery?

  11. 11

    Image overlay using :after pseudo-element

  12. 12

    Select the first CSS element using a pseudo class

  13. 13

    CSS, using display:table with before pseudo element

  14. 14

    Insert &times; using CSS pseudo element

  15. 15

    Creating a shape using a :before pseudo element in CSS

  16. 16

    CSS - Using a pseudo-element for an image mask

  17. 17

    Cannot rotate :after pseudo-element using CSS in IE8

  18. 18

    Using :after pseudo element after anchor element - browser differences

  19. 19

    Toggle a pseudo-element using jQuery

  20. 20

    Toggle a pseudo-element using jQuery

  21. 21

    CSS: <a> tags disabled after using pseudo elements

  22. 22

    How do I modify a css element with Jquery after appended with Jquery to a div?

  23. 23

    How do I modify a css element with Jquery after appended with Jquery to a div?

  24. 24

    Using jQuery to show/hide an :after pseudo class

  25. 25

    Benefit of using jQuery to modify CSS

  26. 26

    Modify the html by using jquery css

  27. 27

    modify pseudo select :after in javascript

  28. 28

    CSS ::before ::after pseudo-element of class not working

  29. 29

    CSS centering content add by pseudo-element after

HotTag

Archive