qadenza
qadenza

Reputation: 9293

create variable name from checkbox data

<input class='setupcheck' type='checkbox' data-x='xpslider'>
<input class='setupcheck' type='checkbox' data-x='xp323'>
<input class='setupcheck' type='checkbox' data-x='xp525'>  

I need for each checkbox to create a variable named as its data('x') and give it a value 1 if it is checked, else - value 0;

Something like this:

$('.setupcheck').each(function(){
    if ($(this).is(':checked')){
        variable named $(this).data('x') = 1;
    }
    else{
        variable named $(this).data('x') = 0;
    }
}
});

Any help?

Upvotes: 0

Views: 354

Answers (1)

Manish Jangir
Manish Jangir

Reputation: 5437

If you want them global variables then use:

var storeObj = window;

$('.setupcheck').each(function(){
    if ($(this).is(':checked')){
        storeObj[$(this).data('x')] = 1;
    }
    else{
        storeObj[$(this).data('x')] = 0;
    }
});

Now check console.log(xpslider);

Or if you want to store them in separate then:

var storeObj = {};

and use as console.log(storeObj.xpslider);

Upvotes: 2

Related Questions