Dynamically create variable in javascript and assign value

Hi to all.

I have checkboxes which is generated within loop of records come from db and append the id with the name and id like this

<input type=checkbox name=CB.<?=$id?> name=CB.<?=$id?> >

Now in extension js file i want to check if check box is checked then create the variabel like CB1 (1 is the id) and the i want to assign value to this variable

i did something like this

[code ]
//arrayLength is the no of check boxes

for(var i= 0 ; i <arrayLength; i++)
{
var val_1=document.getElementById(‘CB’+valuearray[i]).value;

var CB+valuearray[i]=val_1;

alert(CB+valuearray[i]);



how i can do this.

regards.

Currently the generated element assigns two name attributes. This is bad. It should assign an id attribute and a name attribute.

Why are you wanting to create variables for each checked element? That’s normally a bad idea to have so many global variables floating about. If you can please explain your desired intentions, more appropriate solutions can be found for you.

You can do this:


var varname = 'CB1';
window[varname] = 'my value';

You can also use the eval() function, but I prefer the window method.

thanks for quick reply

Actually i need variable like this CB123 (123 is the id which come dynamicall from html ) and then is get this variable in php like CB<?=$db->(‘id’))?> .

I want to handle it with array how can i make an array in javascript like this

array(8) {
[0]=>
string(2) “12”
array(4) {
[“Category_ID”]=>
string(2) “31”
[“Category_Name”]=>
string(8) “Armbands”
[“Category_Image”]=>
string(12) “armbands.jpg”
[“Parent_ID”]=>
string(1) “1”
}
[1]=>
string(2) "13’
array(4) {
[“Category_ID”]=>
string(2) “24”
[“Category_Name”]=>
string(14) “Cuff-Bracelets”
[“Category_Image”]=>
string(17) “cuffbracelets.jpg”
[“Parent_ID”]=>
string(1) “1”
}
}

Javascript has something very similar, called JSON


var myArray = [
    ["12", {
        "Category_ID": "31",
        "Category_Name": "Armbands",
        "Category_Image": "armbands.jpg",
        "Parent_ID": "1"
    }]
],
[
    ["13", {
        "Category_ID": "24",
        "Category_Name": "Cuff-Bracelets",
        "Category_Image": "cuffbracelets.jpg",
        "Parent_ID": "1"
    }]
],
...
[
    ...
];

And yes, there is PHP code to convert to JSON
http://www.midorijs.com/php2json.php.txt

still problem to insert dynamic value in array in js

I am using extension js library do build a ajax base site.

First time when my page execute it show all records from my table for example user table with checkboxes with each record

Checkboxe name is dynamic

<input type=“checkbox” name=“CB”.<?=$id?> id=“CB”.<?=$id?> />

each record has some input field to edit the records

admin will check the check box and make the changes in input field .After chang values he has to call extension js fuction l

where i want to get that value and and send as parameter .

after that it will redirect me to same php page where i write the html also

there he select the records for db again from user table and check if ($_Request[‘CB1212’])

then it will update the record in db

thats why i need dynamic variable.

this can be done by creating simple variable but i dont know which checkbox is checked .

i need this very urgent so if u can pls help

thanks