JavaScript’s getElementById is ok to use, I know that. But how about getElementsByClassName? How is the browser support for that?
Through Google I’ve found various homemade solutions for it, like this
As you can see, this post is four years old now, that’s why I am asking. Or should I just use jQuery for stuff like that if it’s available?
First off, you have to identify exactly which version of IE you need to support: IE6, IE7, IE8? We can’t really provide the best set of options without knowing that. If all you need is IE9 and above (IE8 too in some modes), then you can use
document.querySelectorAll(".classname")ordocument.getElementsByClassName("classname").If you need other versions of IE, you can’t go that route. There are shims for
getElementsByClassName()that are less than 30 lines of code that can be used to provide support for it in older versions of IE, but at that point, I’d strongly suggest you just get a selector library that does ALL the cross browser support for you. It sounds like you are already familiar with jQuery which has such a library. If you don’t want/need the rest of jQuery, you can get just a selector library. jQuery uses Sizzle internally which is available all by itself and is quite good. There are also others.Here’s a good resource so you can look up browser compatibility on your own: http://caniuse.com/getelementsbyclassname.