Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6131769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:01:09+00:00 2026-05-23T17:01:09+00:00

So if I have a DOM like this : <div id=foo> <ul><li><a href=#></a></li></ul> </div>

  • 0

So if I have a DOM like this :

<div id="foo">
<ul><li><a href="#"></a></li></ul>
</div>

I understand to grab the div I would do

   document.getElementById("foo");

but how could I grab the a ? I am trying to add a class to the a href.

ME = spoiled by jQuery

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:01:10+00:00Added an answer on May 23, 2026 at 5:01 pm

    The only thing that you can select by there is the tag name (a). You can use the getElementsByTagName method for this:

    var el = document.getElementsByTagName('a')[0];
    el.className = 'foo';
    

    Note that [0] selects the first element found by getElementsByTagName. Obviously this can return multiple elements.

    If you want to search just within #foo, you could do this:

    var foo = document.getElementById('foo'),
        el = foo.getElementsByTagName('a')[0];
    

    This shows that you can use getElementsByTagName to search within the context of another element.


    One final method — that isn’t supported in all browsers (i.e. Firefox 3 or before, IE 7 or before) — is querySelectorAll, which allows you to use CSS selectors much as you would in jQuery. For instance:

    var el = document.querySelectorAll('#foo a')[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a structure like this: <body> <div data-role=page data-dom-cache=true id=foo>...</div> <div data-role=page
So I have a DOM document that looks essentially like this <categories> <category id=1/>
My DOM looks like this: <div class=stuff> <a href=#><img src=></a> <b>hello</b> <a href=#><img src=></a>
I have a DOM like this: <div id=book-container> <div id=book-inner> Contents of slidedown </div>
I have a few <li> tags inside a <div> like this: <li> <a href=link1>
I have a DOM structure that looks like this: <div id=comment-1234 class=comment>...</div> <div id=comment-2391
If we have an example structure like this: HTML <div id=foo>hello foo</div> JS var
Assuming I have HTML similar to this: <div class=fooContainer> <div class=barContainer> <a href=#>foo-bar</a> </div>
I have a DOM structure that looks like this: <div class=chapter> <section></section> <section></section> </div>
I have a DOM element like this: <div id='master-value'>Apples</div> I have many other elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.