Search code examples
javascriptjquerybusiness-logic

Case-insensitive attribute-value selector with Jquery


I need to get the value of the content attribute of a certain meta tag.

var someContent = $("meta[name=someKindOfId]").attr("content");

is how I usually do it. For business reasons, someKindOfId may be somekindofid. It could be other combinations of cases as well. I don't know.

What is the best way to search for this meta tag? Adding an id or other identifier is out of the question.


Solution

  • You could use the jquery filter function like so

    var meta = $('meta[name]').filter(function() {
        return this.name.toLowerCase() == 'somekindofid';
    });
    

    Based upon CSS selector case insensitive for attributes

    http://jsfiddle.net/nickywaites/mkBvC/