dimanche 13 septembre 2015

Display value of checkbox in textarea

I have here a code that will display the value of the checkbox when checked. My problem is I don't know how I will add up all the numbers, that when I checked a checkbox the number or value will display automatically to the text area and when I checked another checkbox it will add up to the first checkbox that I checked and when I checked another checkbox it should also add up from the first two checkbox. How will I do that?

                <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
            <html>
            <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <script type="text/javascript">
            function add_sub(el)
            {
                if (el.checked)
                {
                    var total = el;
                    total.form.elements['type'].value+=el.value;
                }
                else
                {
                    var re=new RegExp('(.*)'+el.value+'(.*)$');
                    el.form.elements['type'].value=el.form.elements['type'].value.replace(re,'$1$2');
                }
            }
            </script>
            </head>

            <body>
                <form name="form1" method=post>
                    <textarea name="type" rows="5" cols="35" onclick="this.focus();this.select();"></textarea><br>
                    <input type="checkbox" name="mis1" id="id1" value="1" onclick="add_sub(this);"><label>1</label><br>
                    <input type="checkbox" name="mis2" id="id2" value="2" onclick="add_sub(this);"><label>2</label><br>
                    <input type="checkbox" name="mis6" id="id3" value="3" onclick="add_sub(this);"><label>3</label><br>
                    <input type="checkbox" name="mis6" id="id4" value="4" onclick="add_sub(this);"><label>4</label>
                </form>
            </body>




Aucun commentaire:

Enregistrer un commentaire