Setting viewBox attribute using jQuery

I have an element <svg>on my page and would like to give it a viewBox attribute . When I try this with jQuery, like this:

$('svg').attr('viewBox', '0 0 800 400');

It almost works, but gives the element the attribute "view b ox" (note the lower case "b"). This attribute requires the camel body to work, at least in Chrome, where I tested it. Are there any workarounds?

+4
source share
1 answer

I solved this with the built-in Javascript setAttributehint @Mat,

$('svg').removeAttr('viewBox');
$('svg').each(function () { $(this)[0].setAttribute('viewBox', '0 0 800 400') });
+12
source

All Articles