Recent

Author Topic: Checkbox javascript problem  (Read 3797 times)

_Julia_

  • Newbie
  • Posts: 2
Checkbox javascript problem
« on: January 24, 2015, 06:29:29 pm »
Hello,

i'm making this code and i m trying to create code that can count the checkboxes together.

for now i have writen this code : not important verbs are in my language so ignore that .. :D

i m looking for any ideas how can i change this code to be able to count both checkboxes for now i can only pick one and when i do both my reasults came wrong ... it counts only 1.  ;)

so i would be glad if anyone could help me with that.

so my question is how to rewrite the code to be able to count both checkboxes into the result. thanks { if there are more then 2 then ofc it have to count all of them. :D }

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
   "http://www.w3.org/TR/html4/frameset.dtd">
<HTML>
<HEAD>
<TITLE>A simple frameset document</TITLE>
<script>
function myFunction() {
   var a = document.getElementById("a").value;
   var b = document.getElementById("b").value;
   var e = document.getElementById("e").value;
   var w = document.getElementById("w").value;
   var typ = document.getElementsByName("typ");
var z = 0;
    for (var i = 0; i < typ.length; i++) {
        if (typ.type === 'checkbox' && typ.checked) {
        z = typ.value;       
    }
  }
   
   

   
   alert((a*1+e*1-w*1)*15/100-24840*1-b*1*11604- z);
}
</script>
</HEAD>
<body>
   <h1>Dan z prijmu fyzickych osob</h1>
   <form>
   hruby prijem:<br>
   <input type="text" name="a" id="a">
   <br/>
   uhrn pojist. zaplaceneho zamesttnavatelem za zamestnance:<br>
   <input type="text" name="e" id="e">
   <br/>
   odecitatelne polozky slevy:<br>
   <input type="text" name="w" id="w">
   <br/>
   manzelka manzel v domacnosti: <br>
   <input type="checkbox" name="typ" id="q" value="10" >
   <br/>
   studium: <br>
   <input type="checkbox" name="typ" id="x" value="50" >
   <br/>
   
   dite:<br>
   <input type="text" name="b" id="b">
   <br/>

   


   </form>
   <button onclick="myFunction()">Click me</button>
</body>
</html>

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Checkbox javascript problem
« Reply #1 on: January 24, 2015, 07:49:34 pm »
instead of assigning the value to "z" variable, you should increase its value, just replace
Code: [Select]
  z=typ.value;
with
Code: [Select]
  z+=typ.value;

_Julia_

  • Newbie
  • Posts: 2
Re: Checkbox javascript problem
« Reply #2 on: January 27, 2015, 10:28:50 pm »
well i tried but the code it's still not working right the same problem that it counts only the 1 possibility.  :'(

i tryed everything but i cant find anything in pure javascript that do that ... blew :(


TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: Checkbox javascript problem
« Reply #3 on: January 27, 2015, 11:00:15 pm »
Isn't this forum a strange place to come for help about javascript?  ::)
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Checkbox javascript problem
« Reply #4 on: January 27, 2015, 11:19:32 pm »
well i tried but the code it's still not working right the same problem that it counts only the 1 possibility.  :'(
Your loop is correct, but elements of "typ"-array are misused.
This is how it should be.
Code: [Select]
   var z = 0;
   for (var i = 0; i < typ.length; i++) {
        if (typ[i].type === 'checkbox' && typ[i].checked) {
        z += parseInt(typ[i].value);       
    }
note: that value is now wrapped into parseInt. Otherwise you'd see an unexpected 1050, instead of 60 as value of "z"
« Last Edit: January 27, 2015, 11:21:15 pm by skalogryz »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Checkbox javascript problem
« Reply #5 on: January 27, 2015, 11:20:14 pm »
Isn't this forum a strange place to come for help about javascript?  ::)
well... isn't it "Programming / Network and Web Programming" sub section ? ;)

 

TinyPortal © 2005-2018