LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2009, 11:43 PM   #1
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Rep: Reputation: 15
Javascript - trouble using array with getElementById


I have an array of select boxes in a web page, named:
item0
item1
...
item9

I need to fill these boxes with a list of options. The same list goes into each select box. I have tried to do this with arrays but my lists appear empty. When I don't use arrays, things work ...

I'll cut the code down a bit to save space ...

Here is the working code:
var id
var name
var s0
var s1
var s2

// Here I'm using PHP to generate some of the code for me
for ($i=0; $i<9; $i++)
{ echo "s$i=document.getElementById(\"item$j\")\n";
}
// querydb is a wrapper that queries MYSQL and reports errors
$query = querydb("select id, name from items");
while ($data = mysql_fetch_array($query))
{ echo "id = \"$data[0]\"\n";
echo "name=\"$data[1]\"\n";
for ($i=0; $i<9; $i++)
echo "s$i.add(new Option(name, id), null)\n";
}

// -----------------------------
Forgive me if that code has come logic errors caused by cutting it down for this site ... the actual code is considerably more complex, and I only want to show the relevant pieces.

Now ... Here's the same code using arrays which DOESN't work ...

var id
var name
var s = new array[9]

// Here I'm using PHP to generate some of the code for me
for ($i=0; $i<9; $i++)
{ echo "s[$i]=document.getElementById(\"item$j\")\n";
}
// querydb is a wrapper that queries MYSQL and reports errors
$query = querydb("select id, name from items");
while ($data = mysql_fetch_array($query))
{ echo "id = \"$data[0]\"\n";
echo "name=\"$data[1]\"\n";
for ($i=0; $i<9; $i++)
echo "s[$i].add(new Option(name, id), null)\n";
}

// -----------------------------

What it looks like what's happening is the s[$i].add part is the problem, my assumption is that it's trying to call a array.add() function, instead of document.getElementByID(...).add function.
Anyone know how I could fix this?

Thanks
 
Old 06-17-2009, 02:43 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,450

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
PHP Code:
// Here I'm using PHP to generate some of the code for me
for ($i=0$i<9$i++)
{ echo 
"s[$i]=document.getElementById(\"item$j\")\n";

I don't know if this is just a typo, but the element you get is "item$j" - where is $j?

Also, isn't it easier to look at the HTML instead of the PHP? Does it make any Javascript errors?

Edit:
var s = new array[9]

This is also incorrect, try this instead:
var s = new Array(9);

Last edited by Guttorm; 06-17-2009 at 02:46 AM.
 
Old 06-17-2009, 10:36 PM   #3
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Original Poster
Rep: Reputation: 15
Talking

Quote:
Originally Posted by Guttorm View Post
This is also incorrect, try this instead:
var s = new Array(9);
YES, that's it! Well, that's the problem you get when you are writing a program using 4 different languages ... you make a stupid mistake like that Although, I'm kind of baffled as to why the mistake wasn't reported in the exception handler, but who cares, the code works now ... Thank you very much for that.

As for your other statements ... The $j wasn't a typo - but I accidently left out the calculation of $j, and yes, I suppose I should have pasted the code generated by PHP instead of pasting the PHP code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getElementById doesn't work in IE when element is created after page load mohtasham1983 Programming 2 02-09-2008 05:18 AM
JavaScript in firefox trouble Reegz Programming 1 09-16-2006 06:04 AM
Javascript array to PHP using cookies problem climbingmerlin Programming 10 03-09-2006 10:06 AM
telephone number array in javascript esteeven Programming 2 06-08-2005 04:02 PM
returning an array from a function.. javascript sonesay Programming 1 06-07-2004 05:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration