10

I am doing this with jQuery :

@xmlOut = $('<rules />')
@xmlOut.attr('xsi:schemaLocation','test')

I get this :

<rules xsi:schemalocation='test'></rules>

The "L" is not uppercase anymore...

3 Answers 3

10

There is a ticket http://bugs.jquery.com/ticket/11166

Alternatively, you can add attribute hook (with lowercase name) to jQuery in order to use desired setter method. For example:

$.attrHooks['viewbox'] = {
    set: function(elem, value, name) {
        elem.setAttributeNS(null, 'viewBox', value + '');
        return value;
    }
};

Then you can set the attribute case sensitive with .attr():

$('svg').attr('viewBox', '0 0 100 100');
Sign up to request clarification or add additional context in comments.

Comments

5

Try using plain Javascript's setAttribute which is not case sensitive.

@xmlOut.get(0).setAttribute('xsi:schemLocation', 'test');

4 Comments

I thought setAttribute lowercases attribute names. I'm pretty sure I've seen that behaviour before.
Glad I could help. Good question by the way.
Example : test = $('<div />'); test.get(0).setAttribute('TEST','lala') => <div test=​"lala">​</div>​
This is incorrect according to the spec: "The attribute name is automatically converted to all lower-case when setAttribute() is called on an HTML element in an HTML document." developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
4

Kevin's answer is incorrect, .setAttribute() will change the attribute name to lowercase.

Instead, use element.setAttributeNS() with an empty string for the first parameter.

@xmlOut.get(0).setAttributeNS('', 'xsi:schemaLocation','test')

https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttributeNS

3 Comments

Thank you so much for this, you should be on top. Actually, this is the only cross-browser solution! To add XML styles to my DataTables.net Excel export project, I had to switch from .innerHTML and .insertAdjacentHTML (both not recognized in Internet Explorer 11) to elt = document.createElement("numFmt") + elt.setAttribute("formatCode", "0") (lowercased in Chrome and Firefox) + numFmts.appendChild(elt), to eventually elt .setAttributeNS("", "formatCode", "0") !
Precision: as per stackoverflow.com/a/26996439/5426777, you need to use .createElementNS() along with .setAttributeNS()
Also see my complete answer here: stackoverflow.com/a/50719888/5426777. Hope it will help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.