Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

have jQuery ignore case in attribute/data names?

We're using HTML5's data-* attributes for some of our client side interaction set up. jQuery uses these to do its thing.

The catch is that the HTML coming in may vary. Obviously this is the problem that should be fixed but I'm not always in control of the HTML being produced, unfortunately.

The question:

Given these two tags:

<a data-sampleAttributeName="example">

<a data-sampleattributename="example">

Is there a clever way to treat them as one and the same?

The best I've come up with is something like this:

var theAttribute = ($myobject).data('sampleAttributeName');

if (($myobject).data('sampleAttributeName')){
    theAttribute = ($myobject).data('sampleAttributeName')
} else {
    theAttribute = ($myobject).data('sampleattributename')
}

I could turn that into a function that I could just pass the camelCase version to and check for both as well. I was just wondering if there was a cleaner built-in feature in jQuery to ignore the case of the data (or attr) value.

like image 735
DA. Avatar asked Oct 03 '11 22:10

DA.


People also ask

Is jQuery case sensitive?

Yes, it is.

Is jQuery ID selector case sensitive?

jQuery attribute value selectors are generally case-sensitive.


1 Answers

For both the variations given here you should get the value using

.data('sampleattributename')

The camel casing ( .data('sampleAttributeName')) is for when the attribute is like this:

<a  data-sample-attribute-name="something">Anchor</a>

Check this jsfiddle

like image 136
Hari Pachuveetil Avatar answered Oct 19 '22 10:10

Hari Pachuveetil