<span id="opensidebarid" class="icon"></span>
CSS:
.icon:before {
content: 'close';
}
I want to change the content of :before
using the ID from JavaScript (jQuery). How it is possible? I have tried with prop
but not working at all.
<span id="opensidebarid" class="icon"></span>
CSS:
.icon:before {
content: 'close';
}
I want to change the content of :before
using the ID from JavaScript (jQuery). How it is possible? I have tried with prop
but not working at all.
You can set an attribute in your css to get content data from there and then you can change that attribute using jQuery
, since you cannot change the content of a :before
element directly:
CSS:
.icon:before {
content: attr(data);
}
jQuery:
$(document).ready(function(){
$elem = $("#opensidebarid"); // Get element by ID.
elemClass = $elem.attr("class"); // Get the class of that element in order to change content.
$('.' + elemClass).attr('data', "open"); // Change attribute value of content.
});
After thinking a bit, I understood your question. ::before is a pseudo-selector, not a DOM element. So you can’t select it via jQuery.
You can toggleClass via jQuery if would like to change the ::before.
You could change it by creating a toggle class. For example:
.icon-toggle {content: "example" !important}
Then you can create either onclick or some action to toggle the change.
$("#opensidebarid").toggleClass("icon-toggle")
If you could write more detailed question I could help you more.