473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamically dependant listBoxes

I am have used php to create a two sets of arrays of listboxes, each
of the listbox in the array have a unique ID. One of the list boxes
are dependant on the other one. I have written the code in javascript,
well changed existing code. I need to be able to select the element by
ID rather than name. The code was working before I changed things to
"getElementByID". I am not very good at javascript so I am not sure if
I am on the right track, currently nothing is happening.

This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i;
var Name = 'lsName' + i;
Type = document.getElementByID(Type).options[document.getElementByID(Type).selectedIndex].value;

// this bit resets the name select list to nothing.
while (document.getElementByID(Name).options.length > 1)
document.getElementByID(Name).options[0] = null;
// this bit populates the name select list with the required cities.
if (Type.length > 0) {
current_array=Names[Type];

for (j=0;j<current_array.length;j++) {
var optionName = new Option(current_array[j], current_array[j],
false);
document.getElementByID(Name).options[document.getElementByID(Name).length]
= optionName;
}
}
}

This is the bit that calls it:
<select name="lsType[]" onChange="swapName(this.form,'.$i.')"
id="lsType'.$i.'">

Any suggestions would be welcome
Thanks in advance!
Jul 20 '05 #1
2 2547
"nadia" <ns***@student.cpit.ac.nz> wrote in message
news:9c*************************@posting.google.co m...
<snip>
This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i; <snip> This is the bit that calls it:
<select name="lsType[]" onChange="swapName(this.form,'.$i.')"
id="lsType'.$i.'">

<snip>

When you build the Type local variable to use as the ID you are not
including the two single quote characters that you have used in the ID
attribute string. Those single quotes are illegal characters in HTML
terms, as is the $ and the [ and ] in the name attribute so even if you
do include them in when building the Type local variable you should not
have any expectation of the result working on more than just some (if
common) HTML browsers.

From the HTML specification:-
<quote>
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").
</quote>

Richard.
Jul 20 '05 #2
ns***@student.cpit.ac.nz (nadia) writes:
The code was working before I changed things to
"getElementByID".
Then it is probably because it is with a lower case "d":
document.getElementById
^ This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i;
var Name = 'lsName' + i;
Type = document.getElementByID(Type).options[document.getElementByID(Type).selectedIndex].value;
I would split this into two lines:
var selElem = document.getElementById(Type);
var selValue = selElem.options[selElem.selectedIndex].value;

(purely for style, I don't want to reuse variables more than necessary).
<select name="lsType[]" onChange="swapName(this.form,'.$i.')"
id="lsType'.$i.'">


I assume this is part of a PHP string declaration, and the the value
of $i is concatenated with the rest of the string. Be aware that not
everybody in this newsgroup knows PHP (I know only a little), and that
the PHP code used to generate the page really isn't interesting to us.
What is interesting is the HTML that is generated, since that is where
the bug is.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will | last post by:
Hi, I have a problem trying to validate dynamically created html form elements using javascript. I have dynamically created a check box using ASP for each record in a recordset and have given each...
1
by: mlarson | last post by:
Hello, I'm working on a page that dynamically creates listboxes and the "options" that are added to the listbox. A user can then click on buttons to either add or delete the "options" from one...
4
by: bill yeager | last post by:
I have several template columns inside of a datagrid. Inside of these template columns are databound listboxes: <asp:TemplateColumn HeaderText="Crew Chiefs"> <ItemTemplate> <asp:listbox...
0
by: Terry D | last post by:
I'm having an issue with an ASP.NET page (VS.NET 2003, VB.NET, Oracle back end). The page uses the standard VS.NET grid to display the records from a particular table. The user can edit certain...
0
by: Luis Esteban Valencia | last post by:
have a problem and I'm not sure how to handle/fix it. I have three listboxes on my page. The first listbox has a list of software products. When you select an item in the Products listbox, then...
0
by: KBuser | last post by:
I'm building an internal site which will allow for extremely customizable queries to be run against our SQL Server (2000) DB. The page is done in ASP .net 2.0, with C# code behind. The initial...
1
by: KBuser | last post by:
I'm building an internal site which will allow for extremely customizable queries to be run against our SQL Server (2000) DB. The page is done in ASP .net 2.0, with C# code behind. The initial...
7
by: Csanád | last post by:
Hello, I have a form: http://www.steinrogan.com/SecureSite_v2_beta/new_item_single.html The idea is that every time the "Add a Dependant Item" button is clicked, the Item whose "Add a...
8
by: Csanád | last post by:
Hi, Here's my form: http://www.steinrogan.com/SecureSite_v2_beta/addthis.php When I add two or more Dependant Items and remove one that's not the last that I added, I won't be able to remove...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.