261

I have the following css:

.pageMenu .active::after {
    content: '';
    margin-top: -6px;
    display: inline-block;
    width: 0px;
    height: 0px;
    border-top: 14px solid white;
    border-left: 14px solid transparent;
    border-bottom: 14px solid white;
    position: absolute;
    right: 0;
}

I'd like to change the border-width of the top, left, and bottom border using jQuery. What selector to I use to access this element? I tried the following but it doesn't seem to be working.

$('.pageMenu .active:after').css(
        {
            'border-top-width': '22px',
            'border-left-width': '22px',
            'border-right-width': '22px'
        }
    )
4
  • 2
    Have a look at this answer stackoverflow.com/questions/5041494/… Commented Jul 22, 2013 at 13:36
  • 3
    You can't via JQuery, but you can with JavaScirpt (Accessing CSS Rules) stackoverflow.com/questions/15087736/… Commented Jul 22, 2013 at 13:43
  • @AliBassam What you just said makes no sense. jQuery IS Javascript (Actually, a convention library for it, but I hope you get the point). Commented Mar 6, 2014 at 14:31
  • 1
    Here's a superb solution with multiple options, including @blazemonger's below: stackoverflow.com/questions/5041494/… Commented May 30, 2014 at 22:49

3 Answers 3

341

You can't manipulate :after, because it's not technically part of the DOM and therefore is inaccessible by any JavaScript. But you can add a new class with a new :after specified.

CSS:

.pageMenu .active.changed:after { 
/* this selector is more specific, so it takes precedence over the other :after */
    border-top-width: 22px;
    border-left-width: 22px;
    border-right-width: 22px;
}

JS:

$('.pageMenu .active').toggleClass('changed');

UPDATE: while it's impossible to directly modify the :after content, there are ways to read and/or override it using JavaScript. See "Manipulating CSS pseudo-elements using jQuery (e.g. :before and :after)" for a comprehensive list of techniques.

Sign up to request clarification or add additional context in comments.

2 Comments

Here is a great article with three options for editing pseudo-elements in js. pankajparashar.com/posts/modify-pseudo-elements-css
73

You can add style for :after using HTML. For example:

var value = 22;
body.append('<style>.wrapper:after{border-top-width: ' + value + 'px;}</style>');

1 Comment

In jQuery: $( "<style>.wrapper:after { border-top-width: " + value + "px; }</style>" ).appendTo( "head" )
15

If you use jQuery built-in after() with empty value it will create a dynamic object that will match your :after CSS selector.

$('.active').after().click(function () {
    alert('clickable!');
});

See the jQuery documentation.

6 Comments

this doesn't work, nice idea though
This works for me in Firefox, Chrome and Safari
Doesnt work for CSS but lets me target the click event on a the :after tag
jquery after is not meant to manipulate ":after" css property, but the element itself.
your link to jQuery documentation explains that your response is just wrong
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.