I'm trying to manipulate the svg 'viewBox' attribute which looks something like this:
<svg viewBox="0 0 100 200" width="100" ...> ... </svg>
Using
$("svg").attr("viewBox","...");
However, this creates a new attribute in the element called "viewbox". Notice the lowercase instead of intended camelCase. Is there another function I should be using?
you want to make sure you remove the attr if it already exists before manipulating it
and then recreating it
You could use jQuery hooks:
Now jQuery will will use your setter/getters to manipulate those attributes.
Note that
el.attr('viewBox', null)
would have failed; your hook setter won't be called. Instead you should use el.removeAttr('viewBox').Per http://www.w3.org/TR/xhtml1/#h-4.2 "XHTML documents must use lower case for all HTML element and attribute names."
So in order to avoid having the attribute convert to lowercase in an XHTML document you need to create the element specifying a namespace using
document.createElementNS()
, like:If you plan to add a
<use/>
element you also need to specify the namespace while creating the element as well as thexlink:href
attribute, like:I was able to use pure javascript to get the element and set the attribute by using
and