I have been stuck on this for a VERY long time. I have a HTML element with id menuButton
- this button is assigned a triangle through CSS that is set with a script because I need to have the capability to control the color of my svg fill from a color code given to my with a URL parameter.
var urlParams = new URLSearchParams(window.location.search);
var color = urlParams.get("color");
var backgroundColor = urlParams.get("backgroundColor");
var arrowOpen = '<svg aria-hidden="true" width="100" focusable="false" data-prefix="fas" data-icon="play" class="svg-inline--fa fa-play fa-w-14" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="#' + color + '" d="M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z"></path></svg>';
var style = document.createElement('style');
style.type = "text/css"
if (style.styleSheet) {
style.styleSheet.cssText = "#menuButton { background: url('data:image/svg+xml;utf8," + arrowOpen + "') }";
} else {
style.appendChild(document.createTextNode("#menuButton { background: url('data:image/svg+xml;utf8," + arrowOpen + "') }"));
}
document.getElementsByTagName('head')[0].appendChild(style);
<!-- CSS that is generated in my DOM -->
<style type="text/css">
#menuButton { background: url('data:image/svg+xml;utf8,<svg aria-hidden="true" width="100" focusable="false" data-prefix="fas" data-icon="play" class="svg-inline--fa fa-play fa-w-14" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="#FF4500" d="M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z"></path></svg>') }
</style>
Yet nothing appears in my window.. I cannot seem to fathom why....