SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    England
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with forms Please

    i have got an order form sort of thing on my site but at the moment its rubbish. I was wondering ifyou can do the following;

    when you click on a checkbox it adds a value to a text box. There are bout 5 different checkboxes so it would need to add different values for each checkbox. the values are numbers.

    any help appreciated

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">
    
    form {
    	font: 14px monospace;
    }
    input {
    	width: 50px;
    	font: normal 12px monospace;
    	text-align: center;
    	margin: 1px;
    	border: 1px black solid;
    }
    
    </style>
    <script type="text/javascript">
    
    function addboxes(e)
    {
    	var e = e || window.event; 
    	var el = e.target  || e.srcElement; 
    	if (el.nodeName.match(/^input$/i) && el.getAttribute('name').match(/^subval$/))
    	{
    		var oForm = el.parentNode,
    		i = oForm['subval'].length,
    		sum = 0;
    		do
    		{
    			cb = oForm['subval'][--i]
    			if (cb.checked)
    				sum += Number(cb.value) || 0;
    		}
    		while (i);
    		document.getElementById('sum').setAttribute('value', sum);			
    	} 
    }
    
    onload = function()
    {
    	var oBody = document.getElementsByTagName('body').item(0);
    	if (oBody.addEventListener)
    		oBody.addEventListener('click', addboxes, false); 
    	else if (oBody.attachEvent)
    		oBody.attachEvent('onclick', addboxes);
    	else document.onclick = addboxes;
    }
    
    </script>
    </head>
    <body style="margin:60px;">
    <form>
    +3&nbsp; <input type="checkbox" name="subval" value="3" /> <br />
    +5&nbsp; <input type="checkbox" name="subval" value="5" /> <br />
    +11 <input type="checkbox" name="subval" value="11" /> &nbsp;&nbsp;
    <input id="sum" type="text" name="sum" value="0" size="3" readonly="readonly" /><br />
    +22 <input type="checkbox" name="subval" value="22" /> <br />
    +60 <input type="checkbox" name="subval" value="60" /> <br /><br /><br />
    <input type="reset" value="reset" />
    <input type="button" value="source" onclick="alert(addboxes)" />
    </form>
    </body>
    </html>
    
    Last edited by adios; Jul 8, 2004 at 16:12.
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •