Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How change content value of pseudo :before element by Javascript [duplicate]

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?

like image 833
jumancy Avatar asked May 08 '12 08:05

jumancy


People also ask

How do I target a pseudo element in JavaScript?

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.

How do you change 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.

Can I use a before or after pseudo element on an input field?

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.

What is pseudo element CSS?

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.


2 Answers

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; } 
like image 56
Santosh D Avatar answered Oct 16 '22 17:10

Santosh D


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.

like image 30
James Allardice Avatar answered Oct 16 '22 19:10

James Allardice