By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,500 Members | 1,571 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,500 IT Pros & Developers. It's quick & easy.

need help to clear textbox with unchecked checkbox

P: 1
Hi there,
I would like to be able to clear the textbox when we uncheck the checkbox. Is that possible?

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3.   <head>
  4.     <title>cmene be fa no da pagbu</title>
  5.     <script type="text/javascript">
  6.       var Format = {
  7.         currency: function(n, dp, s) {
  8.           return (s || '€ ') + n.toFixed(dp || 2).replace(".",",");
  9.         },
  10.  
  11.         interpolate: function(s) {
  12.           for (var i = 1; i < arguments.length; ++i)
  13.             s = s.replace("%s", arguments[i]);
  14.  
  15.           return s;
  16.         }
  17.       };
  18.  
  19.       var Functional = {
  20.         map: function(f, a) {
  21.           for (var i = a.length - 1, r = []; i >= 0; --i)
  22.             r[i] = f(a[i], i);
  23.  
  24.           return r;
  25.         }
  26.       };
  27.  
  28.       var Dom = {
  29.         getElementsByClassName: (function() {
  30.           function getElementsByClassName_(root, className) {
  31.             return (className(root.className || "") ? [root] : []).concat(entr(root, className));
  32.           }
  33.  
  34.           function entr(root, className) {
  35.             if (typeof className === "string")
  36.               className = (function(cn) {
  37.                 return function(c) {
  38.                   return c.indexOf(cn) >= 0;
  39.                 };
  40.               })(className);
  41.  
  42.             return Array.prototype.concat.apply([], Functional.map(function(e) {
  43.               return getElementsByClassName_(e, className);
  44.             }, root.childNodes));
  45.           }
  46.  
  47.           return entr;
  48.         })()
  49.       };
  50.  
  51.       var TallyPage = {
  52.         sumForm: function(frm, qstr) {
  53.           for (var t = 0, v, e = Dom.getElementsByClassName(frm, "sum"), i = e.length; --i >= 0; )
  54.         if (e[i].type === "checkbox" && e[i].checked)    
  55.               t += ((v = frm.elements[Format.interpolate(qstr, e[i].name)]) ? v.value : 1) * parseFloat(e[i].value, 10);
  56.  
  57.           return t;
  58.         },
  59.  
  60.         updateForm: function(frm, out) {
  61.           (out.firstChild || out.appendChild(document.createTextNode(""))).nodeValue = Format.currency(TallyPage.sumForm(frm, "%s_q"));
  62.         },
  63.  
  64.         init: function() {
  65.           var ud = function() { TallyPage.updateForm(this.form, document.getElementById("output")); },
  66.               v;
  67.  
  68.           Functional.map(function(e) { e.onclick = e.onkeyup = e.onchange = ud; },
  69.               Dom.getElementsByClassName(v = document.forms['sum_form'], function(c) {
  70.                 return c.indexOf('sum') >= 0 || c.indexOf('quantity') >= 0;
  71.               }));
  72.  
  73.           TallyPage.updateForm(v, document.getElementById("output"));
  74.  
  75.           (v = v.elements.subm).parentNode.removeChild(v);
  76.           v = null;
  77.         }
  78.       };
  79.  
  80.       onload = TallyPage.init;
  81.     </script>
  82.   </head>
  83.   <body>
  84.     <form action="/some/summer" id="sum_form">
  85.  
  86.     <input type="checkbox" value="45.25" class="sum" name="101">
  87.     45,25€<input type="textbox" class="quantity" name="101_q" value="1">
  88.    <br> 
  89.     <input type="checkbox" value="50.75" class="sum" name="410">
  90.     50,75€<input type="textbox" class="quantity" name="410_q" value="1">
  91.  
  92.     <span id="output">&nbsp;</span>
  93.     <input name="subm" type="submit" value="Total">
  94.  
  95.  
  96.  
  97.     </form>
  98.   </body>
  99. </html>
Jun 12 '11 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,059
try this.
Expand|Select|Wrap|Line Numbers
  1. <input type=checkbox onclick='if(this.value==checked) docuement.getElementById("clear").value=""'>
  2. <input type=text id='clear'>
  3.  
Jun 13 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.