Hi,
This is more a javascript than a PHP problem, but anyway, if anyone can
help....
I have a script that will dynamically create a series of checkboxes
according to the number of values in the database (a pricelist).
The checkboxes are created with a naming convention from cb1 to cbx (x
being the number of records returned).
Each checkbox calls the onclick action, calling a unique custom
javascript function called add().
How it should work: add() takes the name of the clicked checkbox
(provided as an argument), checks to see if the checkbox is checked or
not, and then, depending on the state, adds the value of the checkbox (a
price) to the value in a textbox.
Problem: If I hardcode the checkbox name, I can do this without any
problems, but I cannot know in advance how many checkboxes will be
created... but it seems that I cannot use a variable as part of the DOM
call...
Is there a solution or a workaround?
Cheers,
Daniel
For example:
This works...
function add()
{
//This will work as cb1 is hardcoded.
alert (document.test. cb1.value);
}
This does not....
function add(cbname)
{
//Here, this will display "cb1".
alert(cbname);
//But here, cbname is taken as a DOM object, and not a variable.
//and raises an error.
alert (document.test. cbname.value);
}
This is more a javascript than a PHP problem, but anyway, if anyone can
help....
I have a script that will dynamically create a series of checkboxes
according to the number of values in the database (a pricelist).
The checkboxes are created with a naming convention from cb1 to cbx (x
being the number of records returned).
Each checkbox calls the onclick action, calling a unique custom
javascript function called add().
How it should work: add() takes the name of the clicked checkbox
(provided as an argument), checks to see if the checkbox is checked or
not, and then, depending on the state, adds the value of the checkbox (a
price) to the value in a textbox.
Problem: If I hardcode the checkbox name, I can do this without any
problems, but I cannot know in advance how many checkboxes will be
created... but it seems that I cannot use a variable as part of the DOM
call...
Is there a solution or a workaround?
Cheers,
Daniel
For example:
This works...
function add()
{
//This will work as cb1 is hardcoded.
alert (document.test. cb1.value);
}
This does not....
function add(cbname)
{
//Here, this will display "cb1".
alert(cbname);
//But here, cbname is taken as a DOM object, and not a variable.
//and raises an error.
alert (document.test. cbname.value);
}
Comment