getElementsByClassName updated and revised

Just a small note to those of you who use my getElementsByClassName script: it has been updated for performance reasons and flexibility reasons. One of the major differences is that tag name and containing element are optional, and if not supplied, will default to * respectively document. This means that the order of the parameters are also changed, so className to look for is the first one, followed by tag and then elm. However, for best performance, I recommend sending in all three parameters as closely specified as possible. Please try it out and let me know if you encounter any problems. You can copy the new script from the post or download the JavaScript file, where it has been added to the previous ones.
Posted in Developing,HTML5/HTML/XHTML,JavaScript |

2 Comments

  • Gunnar says:

    Nice code man! ,

    I´ve developed a set of somewhat similiar methods, for example:

    getParentElementByTagName(strTagName,elemCurrent)

    //returns the closest parent element with tagName: strTagName

    Very useful for example if you are in a td and want to get hold of the table element.

    This is maybe one of the longest method names:

    getElementsByTagAndAttributeNameAndValue(tagName,attName,attValue)

    //returns a collection of elements with tagName: tagName, and which has an attribute with a certain value

    /gunnar

  • Robert Nyman says:

    Gunnar,

    Thanks!

    It always interesting to have the best utility functions available. Regarding your <code>getElementsByTagAndAttributeNameAndValue</code>:

    Something that might interest you then is my getElementsByAttribute.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>