Treehouse: Grow your CSS skills. Land your dream job.

Javascript: Selecting an elment with a class within an element with an id

  • # December 13, 2011 at 1:04 pm

    I’ve tried the following but that didn’t work. No jQuery.


    Turns out this WAS selecting it properly. Something else in my code was broken

    # December 13, 2011 at 1:11 pm

    You’ll have to use an index number, to indicate which one of the found elements with that class you want to target. This will select the first one:


    I’ve never figured out how to select all classes with getElementsByClassName, so that’s why jQuery is so great.

    # December 13, 2011 at 2:10 pm

    Note: getElementsByClassName doesn’t have great browser support

    # March 27, 2012 at 6:04 pm

    What I use instead is the tag name, because in most cases (at least for me) the tags for the class are all the same:

    var tags = getElementsByTagName('a');
    for(i=0;i if(tags.className==='theClassWanted') {
    //code to execute

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.