Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Javascript: Selecting an elment with a class within an element with an id

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35623
    noahgelman
    Participant

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

    document.getElementById('theId').getElementsByClassName('theClass');

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

    #92615
    Senff
    Participant

    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:

    document.getElementById('theId').getElementsByClassName('theClass')[0];

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

    #92627
    jamygolden
    Member

    Note: getElementsByClassName doesn’t have great browser support

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.