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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:54:39+00:00 2026-06-09T11:54:39+00:00

For those who wants the soluce before the question : 1 ) Don’t read

  • 0

For those who wants the soluce before the question :

1 ) Don’t read an element with getElementById() before it’s really created : see windows.onload.

2 ) If you are using XMLHTTPRequest and AJAX stuff, call you’re getElementById() , or unlock this function, in the callback ,(xhr_object.readyState == 4 part of your request).

In my case, i call my page without using the desired callback (noobish Ctrl-C Ctrl-V style).

Here was the question :

I’m facing a strange case in a HTML/Javascript code.
The aim of this code is to get the value of an HTML input (type text) via a JS function.

The context is that a main HTML page loads my all my JS functions, and loads on demand HTML contents in sub divs via HTTPRequest.

The selected code is called after the divs has been loaded.

Here’s the PHP generated Input field that i have to read:

    <input id="listejf" type="text" value="6|7|">

Here’s my JavaScript call :

    listejf=document.getElementById('listejf').value;
    alert(listejf);

This code doesn’t work. Firebug sends me:

TypeError: document.getElementById(“listejf”) is null

The strange thing is that I can make it work if I call the getElementById through an alert like this:

    alert(document.getElementById("listejf"));
    listejf=document.getElementById('listejf').value;
    alert(listejf);

The first alert displays null, but the second one “6|7|”, as expected.

Now, 2 questions:

  • Why does alert make it work ?
  • How can I make it work without throwing alerts everywhere?

Re-edit, the code was gone :

That’s is the main HTML page : main.html

<head>
    <script type="application/javascript" src="./preload.js"></script>
</head>
<body>
    <a href="#" onCLick="CallPagen()">Link</a>
    <div id="targetid"></div>
</body>

preload.js looks like that :

function CallPagen() {
    envoieRequete('./PageN.php', 'targetid');
}

function mytestfunction() {
    listejf = document.getElementById('listejf').value;
    alert(listejf);
}

function envoieRequete(url, id) {
    var xhr_object = null;
    if (window.XMLHttpRequest) xhr_object = new XMLHttpRequest();
    else if (window.ActiveXObject) xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    xhr_object.open("GET", url, true);
    xhr_object.onreadystatechange = function () {
        if (xhr_object.readyState == 4) {
            if (!document.getElementById(id)) {
                alert("id pas trouvé " + id);
            }
            document.getElementById(id).innerHTML = xhr_object.responseText;
            mytestfunction();
        }
    };
    xhr_object.send(null);
}

PageN.php just echoes the inputtext field with the value filled.

  • 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-09T11:54:41+00:00Added an answer on June 9, 2026 at 11:54 am

    “…I can make it work if i call the getelement through an alert like this…”

    This almost always means that you’re making an asynchronous AJAX request.

    “Why does alert make it work ?”

    What happens is that the alert delays the processing of the next line of code long enough for the response to return.

    Without the alert, the next line of code runs immediately, and the element is not yet available.

    “How can I make it work without throwing alerts everywhere?”

    This is a very common issue. The solution is that you need to put any code that relies on the response of the XMLHttpRequest request inside a callback to the request.

    So if you’re making a request through the native API, you’d add a onreadystatechange callback…

    xhrRequest.onreadystatechange = function() {
        if (xhrRequest.readyState === 4) {
            // append the response text to the DOM
    
            listejf=document.getElementById('listejf').value;
            alert(listejf);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a bit confused question for those who don't now advanced SQL... However.
For those who don't know the model. You can read this pdf . I
For those who find it too long, just read the bold lines. My project
A little help from those who’ve used Timthumb before. http://themes.amplus.gambit.ph/wp-content/themes/amplus/functions/timthumb.php?src=http://themes.amplus.gambit.ph/wp-content/uploads/2011/02/12979795614923485443_a1967ff888_b.jpg&w=860&h=300&f=5,255,0,0,0 This image should be
Summary for those who might not want to read that much: How do I
Problem: Customer X is a Windows user who wants to be able to trigger
editing this page for those who want to read this in the future :P
Here is the wiki for those who want to know about Apache CXF. http://en.wikipedia.org/wiki/Apache_CXF
For those who deal with microcontroller programming... I want to take a momentary contact
If I want to search those students who take class Math and John is

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.