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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:51:07+00:00 2026-06-13T22:51:07+00:00

I keep learning very simple things about functions, returns, id and everything, so I

  • 0

I keep learning very simple things about functions, returns, id and everything, so I ran into another problem that looks simple, but I cannot understand why it happens. Check this code:

function test() {
    var text = document.createTextNode("Hello");
    text.id = "t";
    }


var whatIjustwrote = window.document.getElementById("t");
alert(whatIjustwrote);​

Does the getElementById has restrictions to look only for global items? What would be the way to make that alert output the text node inside the function?

Thank you for any comment. The last few days asking things here I have been learning quite a lot!

JSFiddle

  • 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-13T22:51:08+00:00Added an answer on June 13, 2026 at 10:51 pm

    Firstly, getElementById will only return an element, and you’re creating a text node.

    Secondly, it will only return an element that has been added to the DOM. The node you create doesn’t get added to the DOM, so it wouldn’t be found even if it could be.

    Finally, you don’t actually call the test function, so the text node isn’t even created in memory.

    Here’s an updated fiddle that demonstrates getElementById actually working:

    function test() {
        var text = document.createElement("span"); //Create an element
        text.innerHTML = "Hello";
        text.id = "t";
        document.body.appendChild(text); //Add it to the DOM
    }
    
    test(); //Invoke the function (so the element actually gets created)
    var yourElement = document.getElementById("t"); //Get reference to element
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started learning AJAX recently and am trying a very simple project which involves
In learning Core Animation, I learned very quickly that if you don't do it
I am making a very simple game (in the hope of learning a bit
As I'm learning about git, I keep coming across the terms HEAD, master, origin,
I have a VERY simple C# Winforms project that I made using Visual Studio
Just learning a bit of VB and want to search a very simple SQL
I am learning the Passive View pattern to keep my C# WinForms application easier
Keep running into When using the multi-mapping APIs ensure you set the splitOn param
Keep getting this error after inserting a subdatasheet into a query and trying to
Keep going around circles, but I am still unclear about this. Have a feeling

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.