简体   繁体   中英

How to get elements in java script with case insensitiveness?

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

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.

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

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

HTH, Carlo

好吧,我在jQuery中找到的唯一方法是使用过滤器功能的不区分大小写的jQuery属性选择

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM