2

I have the following css:

.example:before {
    content: " $ ";
    margin-left: 4px;
    color: #666;
 }

I'd like to change the 'content' using jQuery. What selector to I use to access this element?

user3652106
  • 48
  • 1
  • 6
  • Your question title shows something **impossible** but your question content shows that it's possible because it's just **to change the content** not **style the :before via jQuery**. It's so confusing. It's better to re-edit the tittle. – King King May 20 '14 at 11:50

1 Answers1

5

You can't directly access :before or :after with Javascript at all. The only way I know to modify them at runtime is to use an attribute, but that sort of defeats the purpose of having css add content in the first place....

HTML

<div class="example" title=" $ ">Something in the div</div>

CSS

.example:before {
    content: attr(title);    /* use the value of the title attribute */
    margin-left: 4px;
    color: #666;
}

Javascript

$(".example").attr("title", " £ ");

working jsfiddle example

Like I said, this sort of negates the whole point of the :before selector and you may as well simply prepend something using Javascript anyway.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67