1

I have a query. I want to get element in java script using it's ID. I can use getElementById. But the problem is that, the ID could be case in sensitive. i.e.

Suppose my ID = "Test"

then I want to get the element with IDs "Test", "test", "tEsT", "TeSt" and all other case combinations. There is a surety that there would be only one element with the ID among all of above.

Can you please help how can I get such element such that I can get the element case insensitively?

Thanks in advance.

  • 3
    **id** must be **unique** – Praveen Jul 28 '14 at 07:21
  • The problem might be that in such a way you might get more than one element. What to do then? – Mirco Jul 28 '14 at 07:23
  • There won't be any element with same ID. so no issue with uniqueness. So Is there a way to do this? – user3883354 Jul 28 '14 at 07:26
  • @Praveen: From the question, "There is a surety that there would be only one element with the ID among all of above." And even if there were many elements each with its own unique case combination, they would all still be considered unique. – BoltClock Jul 28 '14 at 07:45

3 Answers3

4

You could just select all elements with IDs, then use Regex to test them case insensitively:

var result = [].filter.call(document.querySelectorAll('[id]'), function(node) {
    return node.id.match(/test/gi);
});
console.log(result);

Maybe you can narrow down your elements more than a generic [id] selector?

jsFiddle

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0

This REGEX SELECTOR FILTER is what I use to perform similar things.

HTH, Carlo

Carlo Bertuccini
  • 19,615
  • 3
  • 28
  • 39
0

Well the only approach i've found with jQuery is here Case insensitive jQuery attribute selector using filter function

Community
  • 1
  • 1
Gena Moroz
  • 929
  • 5
  • 9