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
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