Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare 2 checkbox'es values

Here http://jsfiddle.net/SW5NH/5/ I created some automatically generated table (DOM + JavaScript).

I want to compare whether checkbox's Input1[z] value (true/false) is equal to Input2's[z] value. If they are not equal value[z] should change.

Tried in several ways (below the table are some test results), but can not achieve it. I would appreciate for any help/comments.

function valCheck() {
    for (var y2 = 0; y2 < empl.length; y2++) {
        var Id1 = 'Option1'+y2+'';
        var Id2 = 'Option2'+y2;
        var input1 = document.getElementById(Id1).checked;
        var input2 = document.getElementById(Id2).checked;
        var value = 'Value'+y2+''; 
        var valEntry = document.getElementById(value);
        var debt = (input1==input2) ? 'no debt' : 'debt';
        document.getElementById('tabinfo').innerHTML = Id1+input1+ ' ' +Id2+input2+ ' ' +(input1&&input2)+value+valEntry+ ' ' + debt;
        document.getElementById(value).createTextNode(debt);
        }
    } 

don't work

UPDATE: Solved. Solution in my last post.

like image 824
Skaidrius Avatar asked Apr 06 '26 20:04

Skaidrius


2 Answers

Your comparsion is right but you should be using 'change' eventListener like this

document.addEventListener('change', valCheck, false);

Check this JSFiddle and share your suggestion.

Also use === instead of ==

like image 185
Praveen Avatar answered Apr 09 '26 08:04

Praveen


Here what you need, DEMO HERE

function checkValue() {
        var a = document.getElementById('a');
        var b = document.getElementById('b');
        if (a.checked != b.checked) {
            document.getElementById('lab').innerHTML="Not equal";
        }

        else
            document.getElementById('lab').innerHTML = "equal";
    } 
like image 20
Sudarshan Avatar answered Apr 09 '26 09:04

Sudarshan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!