Calling a function in a function

Results 1 to 2 of 2

Thread: Calling a function in a function

  1. #1
    Join Date
    Dec 1969

    Default Calling a function in a function

    Hi,<BR><BR>This is going to be dead simple for most people (other than myself). I have sourced two scripts from the messageboard archive which check that an integer value is entered and then sum all integer values. The part i don&#039;t understand is how I call my getTotal() function once a legitimate valueis entered. <BR><BR>Here is the code:<BR><BR>&#060;script language="Javascript"&#062;<BR> function numVal(theField)<BR> {<BR> var val = parseFloat(theField.value);<BR> if (isNaN(val) &#124&#124 (val != parseInt(val)))<BR> {<BR> alert("You didn&#039;t enter an integer");<BR> theField.value = "0";<BR> theField.focus();<BR> }<BR> }<BR>&#060;/script&#062;<BR>&#060;script language="Javascript"&#062;<BR> function getTotal()<BR> {<BR> frm = document.requisition;<BR> total = 0;<BR> for (var i=1; i&#060;= frm.elements.length; i++)<BR> {<BR> total = total + parseInt(frm.elements[i+"Qty"].value;<BR> }<BR> frm.frmTotal.value = total;<BR> }<BR>&#060;/script&#062;<BR><BR>Any help is appreciated!<BR><BR>cheers<BR><BR>ab

  2. #2
    Join Date
    Dec 1969

    Default Keep it simple

    You don&#039;t really need to anything complex.<BR><BR>I assume that numVal() will be called on each OnBlur of each field to be validated or something like that since the parameter is the field object itself<BR><BR>then all you need to do is write this :<BR><BR>&#060;form [attributes here...] OnSubmit="getTotal()"&#062;<BR><BR>when the user clicks on the submit button, the total will be calculated and transmitted to the next page.<BR><BR>OR<BR><BR>If you make to make it even fancier, you can recalculate the total as the fields are changing.<BR><BR>and if that&#039;s the case, then we&#039;ll need to bring slight modifications to your numVal() and getTotal() functions :<BR><BR><BR>function numVal(theField) {<BR> var val = parseFloat(theField.value);<BR> var strErrMsg<BR> <BR> if (isNaN(val) &#124&#124 (val != parseInt(val))) {<BR> strErrMsg = "You didn&#039;t enter an integer")<BR> }<BR>}<BR><BR>function getTotal() {<BR> var frm = document.requisition;<BR> var total = 0;<BR> var strErrMsg<BR> for (var i=1; i &#060; frm.elements.length; i++) {<BR> //The line below assumes fields have a name like "1Qty, 2Qty, /Qty...etc"<BR> strErrMsg = numVal(frm.elements[i+"Qty"])<BR> if(strErrMsg != &#039;&#039;) {<BR> alert(strErrMsg)<BR> frm.elements[i+"Qty"].value = "0";<BR> frm.elements[i+"Qty"]focus();<BR> } <BR> else {<BR> total += parseInt(frm.elements[i+"Qty"].value;<BR> }<BR> }<BR> frm.frmTotal.value = total;<BR>}<BR><BR><BR>...then on each field to be validated you write OnBlur="getTotal()"<BR><BR>(or whatever the event that you are currently using)<BR><BR>I do not guarantee this code will work as-is and it would be in your best interest to review it first.<BR><BR>Eniac

Posting Permissions

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