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 8143039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:59:17+00:00 2026-06-06T12:59:17+00:00

This question has disturbed me for a long time. Sorry if it is a

  • 0

This question has disturbed me for a long time. Sorry if it is a stupid question.

Before, I have known that you can get the elements with a class name

document.body.getElementsByClassName("foo");

And I was so lazy, so I just copied and pasted the code to the other part to do this

document.body.getElementById("bar");

I accidentally found it won’t work. I tested and it says

TypeError: Object #<HTMLBodyElement> has no method ‘getElementById’

So why does it have getElementsByClassName andgetElementsByTagName and all those similar methods, but only no getElementById?

typeof document === typeof document.body   //true

Their types are the same, so they should have the same thing. But it does not seem to be the case here.

  • 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-06-06T12:59:18+00:00Added an answer on June 6, 2026 at 12:59 pm

    You can have multiple elements with the same class name so narrowing down the search to start with a specific node make sense.

    It doesn’t make sense with id because it it should be unique.

    You can have only one id in the document, this why getElementById is a method of document.

    Example:

    <body>
        <div id="start">
            <span class="a">
        </div>
        <div class="a">
        </div>
    </body>
    

    Start searching for class a from the node <div id="start"> will give you one element,
    While if you would have start from the top node- document, it would have ended with two elements.

    Regarding to the typeof comparing:

    typeof 1 == typeof 2 == "Number" // true
    1 !== 2 // true.
    

    typeof only checks for the type, not the value, document and document.body are both objects, but different objects.

    typeof document === typeof document.body === typeof null === "object" // true
    document === document.body // false!!!
    

    As you can see, null and document share the same type, but do they have the same methods…? NO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been bugging me for a long time now but essentially I'm
This question has been puzzling me for a long time now. I come from
This question has two parts: #1 I have a functions.php that is filled with
This question has been asked before - How we can use CTE in subquery
This question has problably been asked before, but it seems like I can't phrase
This question has been brought up before, and I have searched many of the
This question has been asked before ( link ) but I have slightly different
This question has been asked before but i still don't understand it fully so
this question has probably been asked before, but i'm stuck and i've tried a
This question has been asked before, but I would like a little more detail

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.