I'd like to fill an svg
file inside inside an <object>
tag using javascript.
The problem is that .contentDocument
returns null
, therefore I can't fill the svg icon.
JSFiddle : http://jsfiddle.net/8kf36L0j/16/
I'm on the latest version of Firefox and Chrome, I read lots of other posts about this subject but I couldn't find something usefull to fix this issue.
HTML
<object id="svg_image_id" type="image/svg+xml" data="http://openclipart.org/people/StudioFibonacci/kitchen-blender.svg"></object>
Javascript
// Alternativ 1
alert(document.getElementById("svg_image_id").contentDocument); // .contentDocument == null ?
document.getElementById("svg_image_id").contentDocument.getElementById("path241").style.fill="red";
// Alternativ 2
var obj = document.querySelector("#svg_image_id");
var svg = obj.contentDocument.querySelector("svg"); // obj.contentDocument == null ?
svg.style.fill="red";
alert(svg);
Why is contentDocument
returning null
? How can I fill the svg icon to another color ?