Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relative function call 2

Status
Not open for further replies.

crabgrass

Technical User
Aug 29, 2007
111
US
Suppose we had a function that looked like this:

<SCRIPT language="JavaScript">
function totalyear(_year){
document.forms["tippage"].pTotal1.value =
parseFloat(document.forms["tippage"].pLocal1.value) +
parseFloat(document.forms["tippage"].pState1.value) +
parseFloat(document.forms["tippage"].pFed11.value) +
parseFloat(document.forms["tippage"].pFed12.value) +
parseFloat(document.forms["tippage"].pFed13.value);
}
</script>

The variable _year is a number representing the set of values to be added. That is if _year=1 the function is good as shown. However, if _year=2, we want the function to add different controls as in:

<SCRIPT language="JavaScript">
function totalyear(_year){
document.forms["tippage"].pTotal2.value =
parseFloat(document.forms["tippage"].pLocal2.value) +
parseFloat(document.forms["tippage"].pState2.value) +
parseFloat(document.forms["tippage"].pFed21.value) +
parseFloat(document.forms["tippage"].pFed22.value) +
parseFloat(document.forms["tippage"].pFed23.value);
}
</script>

So the question is, How would you revise the function to incorporate the _year value into the controls specification?

Thanks
 
[tt]function totalyear(_year){
with (document.forms["tippage") {
elements["pTotal"+_year].value =
parseFloat(elements["pLocal"+_year].value) +
parseFloat(elements["pState"+_year].value) +
parseFloat(elements["pFed"+_year+"1"].value) +
parseFloat(elements["pFed"+_year+"2"].value) +
parseFloat(elements["pFed"+_year+"3"].value);
}
}[/tt]
 
Code:
function totalyear(_year){
    with (document.forms["tippage"[!]][/!]) {
        elements["pTotal"+_year].value =
        parseFloat(elements["pLocal"+_year].value) +
        parseFloat(elements["pState"+_year].value) +
        parseFloat(elements["pFed"+_year+"1"].value) +
        parseFloat(elements["pFed"+_year+"2"].value) +
        parseFloat(elements["pFed"+_year+"3"].value);
    }
}

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You should also probably check each text input to make sure the value in it is a number, and that it actually has a numeric value in the input, before allowing the user to trigger whatever performs the calculation.

Lee
 
Thanks to all of you. I never thought it would be that easy. Since I'm new to Java, how would you validate the numbers as suggested by trollacious?
 
First off, don't confuse Java with JavaScript - they are 2 COMPLETELY different beasts.

Secondly, since you're new a quick and easy way to validate numbers would involve partially the method that tsuji has already presented to you. When using parseFloat or parseInt, if javascript's parser can not understand the input provided it will return the NaN status (not a number). Javascript already has a built in function for NaN called (you guessed it), isNaN.

Here's an example:
Code:
function check(obj) {
   if ([!]isNaN(obj)[/!]) {
      alert(obj + " is not a number");
   }
   else {
      alert(obj + " is a number");
   }
}

var a = parseFloat("1234");
var b = parseFloat("Im not a number");

check(a);
check(b);




-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
First, it's Java[red]script[/red]. Java is something completely different.

You'd need to create a function that would check the value of each of the inputs that should be numbers only and use it something like:
Code:
function totalyear(_year)
{
var el = document.forms["tippage"].elements;

if (isNaN(el["pLocal"+_year].value) || el["pLocal"+_year].value.length == 0)
  {
  alert('Must be a number');
  el["pLocal"+_year].select();
  el["pLocal"+_year].focus();
  return;
  }

//use something similar for the rest of the inputs

el["pTotal"+_year].value =
        parseFloat(el["pLocal"+_year].value) +
        parseFloat(el["pState"+_year].value) +
        parseFloat(el["pFed"+_year+"1"].value) +
        parseFloat(el["pFed"+_year+"2"].value) +
        parseFloat(el["pFed"+_year+"3"].value);
}

Lee
 
You know what they say about great minds [smile]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
OK, you've all been very helpful. Thanks. One last thing, I've come up with this pair of functions.

function ckNum(obj) {
if (isNaN(obj)) {
obj = 0;
return 0
}
else {
return obj;
}
}
function totalyear(_year){
with (document.forms["tippage"]) {
elements["pTotal"+_year].value =
ckNum(parseFloat(elements["pLocal"+_year].value)) +
ckNum(parseFloat(elements["pState"+_year].value)) +
ckNum(parseFloat(elements["pFed"+_year+"1"].value)) +
ckNum(parseFloat(elements["pFed"+_year+"2"].value)) +
ckNum(parseFloat(elements["pFed"+_year+"3"].value));
}
}

These seem to work fine except that the attempted replacement of the none number with a zero does not work. This puzzels me because in another function, the line

document.forms["tippage"].PmtRequest.value = 0

works fine. So why doesn't it work in the ckNum() function?
 
Have you used Firefox's error console to see if any errors are showing up?

Lee
 
Are you wanting the non-numbers to be replaced in the form? I would guess that p_Total+year field is being calculated correctly, but you're expecting the non-number fields to turn into 0's, right? If that's the case, you're never actually setting the values in the function. Make the following change to clean up the code:

(on a side note, put the code you post between [ignore]
Code:
[/ignore] tags to make it easier to read.

Code:
[gray]//I changed the name of the function cause if you're changing the value you're not just "checking" it[/gray]
function fixNum(obj) {
   var objValue = parseFloat(obj.value);
   if (isNaN(objValue)) {
      [gray]//we set the value of the form field here[/gray]
      [!]obj.value = 0;[/!]
      return 0
   }
   else {
      return objValue;
   }
}

function totalyear(_year){
    with (document.forms["tippage"]) {    
        elements["pTotal"+_year].value =
        fixNum(elements["pLocal"+_year]) +
        fixNum(elements["pState"+_year]) +
        fixNum(elements["pFed"+_year+"1"]) +
        fixNum(elements["pFed"+_year+"2"]) +
        fixNum(elements["pFed"+_year+"3"]);
    }
}

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
I know little to nothing about Firefox. I have it installed and it runs the page the same as IE7. No errors apparent but I am not familiar with the error console, where it is or how to use it.
 
Kaht -
You have diagnosed correctly and provided the help I needed. Thanks
Actually I had thought about passing the object vs the value but since it was working with the value passed and it was the value I wanted to change, I thought it should be OK the way it was.

- Don
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top