Only by applying a CSS rule that's at least as specific and later in the CSS, or more specific. You can't via .style
on the element (because that style isn't applied to that element, it's applied to the pseudo-element before it, which cannot be accessed at all).
For example:
// Say we want to target the second one
var idAllocator = 0;
setTimeout(function() {
var target = document.querySelectorAll("#header .number")[1];
if (target) {
if (!target.id) {
target.id = "__unique__id__" + idAllocator++;
}
var style = document.createElement("style");
style.type = "text/css";
style.textContent = "#" + target.id + ".number::before { content: 'UPDATED'; }";
document.querySelector("head").appendChild(style);
}
}, 800);
#header .number::before {
content: '0810-222-1XXX';
}
<div id="header">
<div class="number">a</div>
<div class="number">b</div>
<div class="number">c</div>
<div class="number">d</div>
</div>
You could take that further by giving the style
element an ID derived from the element's ID so you could update it subsequently if desired:
var idAllocator = 0;
function updateBeforeContent(target, content) {
if (!target.id) {
target.id = "__unique__id__" + idAllocator++;
}
var styleId = "__style__for_" + target.id;
var style = document.getElementById(styleId);
if (!style) {
style = document.createElement("style");
style.type = "text/css";
style.id = styleId;
document.querySelector("head").appendChild(style);
}
style.textContent = "#" + target.id + ".number::before { content: '" + content + "'; }";
}
// Say we want to target the second one
var target = document.querySelectorAll("#header .number")[1];
if (target) {
setTimeout(function() {
updateBeforeContent(target, "UPDATE1");
}, 800);
setTimeout(function() {
updateBeforeContent(target, "UPDATE2");
}, 1600);
}
#header .number::before {
content: '0810-222-1XXX';
}
<div id="header">
<div class="number">a</div>
<div class="number">b</div>
<div class="number">c</div>
<div class="number">d</div>
</div>