Home Javascript does not the true result of Checkbox

# Javascript does not the true result of Checkbox

chouueawaech
1#
chouueawaech Published in 2018-01-13 07:33:41Z
 I'm working with Javascript to see if a checkbox is checked or not. So I coded this: 
cb1

cb2

 So if you check the checkbox within this example, after submitting the form, the true value of checkbox must be shown in the popup alert. But the problem with it is that whenever I try to Submit the form while both checkboxes are checked, I still see the message cb1 = 0 cb2 = 0 on screen. Now why is that?
Nisarg
2#
 You are setting cb.style.checked instead of cb.dataset.checked. Updating that would fix the issue. function toggleCB(cb){ if(cb.dataset.checked == 0){ cb.style.background = "#89D814"; cb.dataset.checked = 1; // << --- This }else{ cb.style.background = "#EEE"; cb.dataset.checked = 0; } }  
cb1

cb2


3#
 Your toggle function, you do cb.style.checked = 1;. It suppose to be cb.dataset.checked = 1;.
Hoargarth
4#
 I guess your problem is your if clause. if(cb.dataset.checked == 0) { cb.style.background = "#89D814"; cb.style.checked = 1; }  You wrote cb.style.checked but I guess what you wanted to write was cb.dataset.checked
 You need to login account before you can post.
Processed in 0.331424 second(s) , Gzip On .