10

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

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

It almost works, but it gives the element a "viewbox" attribute (notice the lower case 'b'). This attribute requires the camel case to work, at least in Chrome where I have tested it. Are there any workarounds?

Ben
  • 1,023
  • 1
  • 7
  • 14
  • 1
    Have you checked out this? I think it's what you're after http://stackoverflow.com/questions/12361971/does-the-attr-in-jquery-force-lowercase – Mat Jul 23 '14 at 14:45

1 Answers1

21

I solved this using @Mat's native Javascript setAttribute tip,

$('svg').removeAttr('viewBox');
$('svg').each(function () { $(this)[0].setAttribute('viewBox', '0 0 800 400') });
Ahmed Salama
  • 2,710
  • 1
  • 9
  • 15
Ben
  • 1,023
  • 1
  • 7
  • 14