javascript - Checkbox value added by default -


i'm attempting make code display divs when checked , add values of checkboxes together. i've managed come want make of checkboxes checked , values added default. can make checkbox checked, however, need value added default well. appreciated. here code: first paragraph
second paragraph
third paragraph

<form name="formex">  <input onclick="clickch(this) ; showpara()" class="classone" type="checkbox" name="one" value="10"> $10.00<br> <input onclick="clickch(this) ; showpara()" type="checkbox"  name="two" value="12"> $12.00<br> <input onclick="clickch(this) ; showpara()" type="checkbox" name="three" value="1"> $1.00<br> <input onclick="clickch(this) ; showpara()" type="checkbox" name="four" value="2"> $2.00<br> <input onclick="clickch(this) ; showpara()" type="checkbox" name="five" value="24"> $24.00<br>  <br> <input id="total" type="text" name="total"> </form> 

my script

<script language="javascript" type="text/javascript"> var total = document.getelementbyid("total") function clickch(caller){ if(caller.checked){ add(caller) } else { subtract(caller) }}  function add(caller){   total.value = total.value*1 + caller.value*1} function subtract(caller){  total.value = total.value*1 - caller.value*1}   function showpara()        {             document.getelementbyid("first").style.display=(document.formex.one.checked) ? "inline" : "none";             document.getelementbyid("second").style.display=(document.formex.two.checked) ? "inline" : "none";            document.getelementbyid("third").style.display=(document.formex.three.checked) ? "inline" : "none";             return true;         } </script> 

jsfiddle: http://jsfiddle.net/tcz6t/1/

you should able add attribute "checked" end of tag have checked default. in other words, if wanted $10 checked default change tag to:

<input checked onclick="clickch(this) ; showpara()" class="classone" type="checkbox" name="one" value="10" checked> $10.00<br> 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -