Store in uppercase using attr () with jquery (case sensitive)

I do this using jQuery:

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

I get this:

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

"L" is no longer in uppercase ...

+7
source share
3 answers

Try using simple Javascript setAttribute , which is not case sensitive.

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

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

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

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

You can then set the case-sensitive attribute with .attr ():

 $('svg').attr('viewBox', '0 0 100 100'); 
+7
source

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

0
source

All Articles