I have the grap constructured by CSS, which is dynamically changes by JS. I show graph max value by pseudo element as:
.graph:before { content:""; //value that want set by JS position:absolute; top:0; left:0; }
That's why I need to set this value by JS. I tried $(".graph:before").css("content", hh);
but it didn't help. How to get that value?
You can't select pseudo elements in jQuery because they are not part of DOM. But you can add a specific class to the parent element and control its pseudo elements in CSS. Show activity on this post. We can also rely on custom properties (aka CSS variables) in order to manipulate pseudo-element.
In general, if we want to change anything in pseudo elements through JavaScript, we do it in the following way: Create CSS classes on element, which will change pseudo elements' UI. Get the element using querySelector. Modify the classes using classList.
The input element has no content in the CSS view, and so has no :before or :after pseudo content. This is true of many other void or replaced elements. There is no pseudo element referring to outside the element.
A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph. /* The first line of every <p> element.
I hope the below snippet might help, you can specify the content
value you want via JS using the CSS attr()
function. Below you have two options: to use JavaScript or jQuery:
jQuery:
$('.graph').on('click', function () { //do something with the callback $(this).attr('data-before','anything'); //anything is the 'content' value });
JavaScript:
var graphElem = document.querySelector('.graph'); graphElem.addEventListener('click', function (event) { event.target.setAttribute('data-before', 'anything'); });
CSS:
.graph:before { content: attr(data-before); /* value that that refers to CSS 'content' */ position:absolute; top: 0; left: 0; }
Update (2018): as has been noted in the comments, you now can do this.
You can't modify pseudo elements through JavaScript since they are not part of the DOM. Your best bet is to define another class in your CSS with the styles you require and then add that to the element. Since that doesn't seem to be possible from your question, perhaps you need to look at using a real DOM element instead of a pseudo one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With