...

View Full Version : jquery: how to make variables from values



lauthiamkok
08-21-2009, 04:24 AM
Hi,
I wonder if i can turn values into variables...

for instance this is the html,



<input type="checkbox" id="var_1" class="categories"/>
<input type="checkbox" id="var_2" class="categories"/>



it is fine if i declare the variable manually like this below,


if ($('#var_1').attr('checked')) {
var_1 = 1;
}else{
var_1 = '';
}

if ($('#var_2).attr('checked')) {
var_2 = 1;
}else{
var_2 = '';
}

but i think it is a bit too much to declare variable one by one when i have a long list of checkboxes.

I thought i could use each() to generate the variables automatically when it has detected other checkboxes in the list.


$(".categories").each(function () {
var $this = $(this);
var id = $this.attr('id');
if ($("#"+id).attr('checked')) {
var id = id;
id = 1;
}else{
var id = id;
id = '';
}
});

but it wont work at all - i think the variables have not been made from values from the code above...

is that a possible to do that??

thanks,
Lau

rnd me
08-21-2009, 09:35 AM
$(".categories").each(function (b,a) {
if(a.id) window[a.id]=a.checked?1:"";
});

lauthiamkok
08-21-2009, 01:09 PM
$(".categories").each(function (b,a) {
if(a.id) window[a.id]=a.checked?1:"";
});

thank you very much. it works now :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum