0

i'm working on a project and try to do something with checkboxes.

when a user click to a checked checkbox total value is - reverse is +

this is my code. i use recal function to do that check box is checked and send it to js as true

 echo "<td><input type=checkbox name='check1' value='".$info['eventCategory']."'  onclick='recal(" . $info['totalEvents'] . ",true)' checked></td><td>" . $info['id'] . "      " . $info['name'] . "</td>";




function recal(val,sum)
{ 
    if(sum)
    {
    var total = document.getElementById("total").innerHTML;
    total+=val;
    document.getElementById("total").innerHTML=total;
   }       
}
</script>

when sum is come as true it doesnt not sum the value :S

2 Answers 2

1

looks to me like a type casting problem - change this line:

var total = document.getElementById("total").innerHTML;

to this:

var total = parseInt(document.getElementById("total").innerHTML, 10);
Sign up to request clarification or add additional context in comments.

4 Comments

is this wrong = if(check1.checked){onclick='recal(" . $info['totalEvents'] . ",true)'}
id use jquery for that: something like: $('#check1').on('change',function(){recal(" . $info['totalEvents'] . ",true);}); - you might need to add this attribute to teh checkbox itself: <input type=checkbox name='check1' id='check1' value....
onclick=if(this.checked){'recal(" . $info['totalEvents'] . ",true)'} what is wrong with that
if it works, then nothing is wrong with it. id just personally use jquery because im more familiar with it and you can do more with less lines of code.
0

Also try looking at this example it should give You more overview what could've go wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.