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

  • SEARCH
  • Home
  • 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

I have a DOM element like this: <div id='master-value'>Apples</div> I have many other elements
I have a DOM structure that looks like this: <div id=comment-1234 class=comment>...</div> <div id=comment-2391
I have some HTML setup like this: <div> <label for=amount><a id=amount-help-icon class=help icon-link href=#><span
Lets say I have a p tag like this: <div id = example> <p
Imagine the HTML is like this: <a id=tour_gallery_link href=#>Click Me</a> <div id=tour_gallery> <a href=image1.png><img
I have an XML file loaded into a DOM document, I wish to iterate
Premise: I've started to study javascript and the DOM and I have this HTML
I have HTML similar to the following in my page <div id=someDiv> <img src=foo.gif
I have this project i'm working on and id like to add a really
In jQuery whenever I encounter something like this: $(div#MyDiv)..... I generally say to the

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.