Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep uppercase using attr() with jquery (case sensitive)

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...

like image 487
Sebastien Avatar asked Nov 20 '12 10:11

Sebastien


3 Answers

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

like image 70
benmod Avatar answered Oct 20 '22 19:10

benmod


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');
like image 25
Evgeniy Generalov Avatar answered Oct 20 '22 17:10

Evgeniy Generalov


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

@xmlOut.get(0).setAttribute('xsi:schemLocation', 'test');
like image 25
Kevin Bowersox Avatar answered Oct 20 '22 18:10

Kevin Bowersox