1

hello everyone I am trying to make a script that steps through a list and appends a score to each record returned by the query. this code below is what's happening in PHP; a input is created where I store the number that's going to be processed in a string; below that there is a span element that displays the text score and another span that contains the actual score. Finally there's an echo that showcases the values generated which look like: score_pad0, score_pad1, score_pad2

echo "<input type='hidden' id='fetch_array' value=".$score_board." />";
echo "<span>score: </span><span id='score_pad$score_board'></span><br />";
echo "score_pad$score_board";

I want to grab the value from fetch_array in javascript. I use the following code for that
var array_id = document.getElementById('fetch_array'); this however returns null when I run it. What am I doing wrong? I've tried to read it out by adding the .value extension. but it only crashed my javascript code.

the onload event is triggered by the a function

function initialize(numbers_rows)
{
for(var i=0; i < numbers_rows; i++)
{   
    var node=document.createElement("span");
    node.id="scoreLabel";
    document.getElementById("score_pad"+array_id).appendChild(node);
}
}
6
  • Are you sure you are trying to access document.getElementById('fetch_array') after DOMLoad? Can you paste the full code of yours? Commented Jun 14, 2012 at 11:30
  • @Tamil I did use the onload event once in the body does that matter at all? I'm not a javascript expert yet :) Commented Jun 14, 2012 at 11:32
  • Ya there is a possibility that the time your javascript code gets executed your DOM might not be ready. As a trial do <body onload='myFunction()'> and wrap your document.getElementById inside myFunction() and see what happens. Commented Jun 14, 2012 at 11:35
  • You cannot have the same id for all the children , may be that is the problem, node.id='scoreLabel' and array_id not defined it should be i Commented Jun 14, 2012 at 11:41
  • @Tamil I've been testing with your advice; and yes that does work; it even enables the use of the .value extension again. so how can I call on multiple functions in a onload event? Commented Jun 14, 2012 at 11:47

2 Answers 2

1

There is a possibility that the time your javascript code gets executed your DOM might not be ready. As a trial do <body onload='myFunction()'> and wrap your document.getElementById inside myFunction()

If does work incase you wish to have multiple handlers for onload event you need to use addEventListener DOC for non IE browsers and attachEvent for IE.

If you could use jQuery then wrap your javascript code around

$().ready(function () {
 // Your Code Goes here &Will be executed after DOM is ready
});

Check this question on SO

Sign up to request clarification or add additional context in comments.

1 Comment

@JeffreyKlinkhamer Have added a useful link on the same issue which I asked when I was a javascript beginner ;)
0

Use

var array_id = document.getElementById('fetch_array').value;

3 Comments

"I've tried to read it out by adding the .value extension. but it only crashed my javascript code" Clearly specified in OP
You should ask the original poster only ;) I jus pointed out that your answer seems to be not helpful
@NaveenKumar thank you for contributing, in the end the .value extension made it's comeback and for filled his role ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.