Help with javascript function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Help with javascript function

Post by sulen »

I have a javascript function that I need to repeat 15 times but I am kind of not sure that the syntax I am using is correct or not. The function works perfectly if I use it one but when I try to use it with an array does the problem occur.

Code: Select all

var i=1;
for (i=1; i<=15; i++) 
{
function startCalc[i](){
  interval[i] = setInterval[i]("calc[i]()",1);
}
function calc[i](){
  one[i] = document.myform.mie_irent[i].value;
  two[i] = document.myform.mie_impfee[i].value; 
	three[i] = document.myform.mie_idcredits[i].value; 
	four[i] = document.myform.mie_irmbs[i].value; 
	five[i] = document.myform.mie_ioinc[i].value; 
  document.myform.mie_itotal[i].value = (one[i] * 1) + (two[i] * 1) + (three[i] * 1) + (four[i] * 1) + (five[i] * 1);
}
function stopCalc[i](){
  clearInterval[i](interval[i]);
}
}

The functions as a stand alone just used without the array is as under. This functions works like a charm. Need help with the mods above.

Code: Select all

function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  one = document.myform.mie_irent1.value;
  two = document.myform.mie_impfee1.value; 
	three = document.myform.mie_idcredits1.value; 
	four = document.myform.mie_irmbs1.value; 
	five = document.myform.mie_ioinc1.value; 
  document.myform.mie_itotal1.value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1);
}
function stopCalc(){
  clearInterval(interval);
}

Any help will be appreciated. Thanks in advance
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with javascript function

Post by Christopher »

Maybesomething like this, but I am not clear what the form data looks like:

Code: Select all

var i=1;
for (i=1; i<=15; i++) 
{
function startCalc(i){
  interval[i] = setInterval("calc("+i+")",1);
}
function calc(i){
  one[i] = document.myform.mie_irent[i].value;
  two[i] = document.myform.mie_impfee[i].value; 
	three[i] = document.myform.mie_idcredits[i].value; 
	four[i] = document.myform.mie_irmbs[i].value; 
	five[i] = document.myform.mie_ioinc[i].value; 
  document.myform.mie_itotal[i].value = (one[i] * 1) + (two[i] * 1) + (three[i] * 1) + (four[i] * 1) + (five[i] * 1);
}
function stopCalc(i){
  clearInterval(interval[i]);
}
}
(#10850)
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

The form is as under

Code: Select all

<form name='myform'>
<? for ($j=1;$j<=15;$j++) { ?>
<input name="mie_irent<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc<?=$j?>();" onBlur="stopCalc<?=$j?>();" />
<input name="mie_impfee<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc<?=$j?>();" onBlur="stopCalc<?=$j?>();" />
<input name="mie_idcredits<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc<?=$j?>();" onBlur="stopCalc<?=$j?>();" /> 
<input name="mie_irmbs<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc<?=$j?>();" onBlur="stopCalc<?=$j?>();" />
<input name="mie_ioinc<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc<?=$j?>();" onBlur="stopCalc<?=$j?>();" />
TOTAL MONTHLY INCOME: $ <input name="mie_itotal<?=$j?>" type="text" class="txtbox1" size="20" />
<? } ?>
</form>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Then you probably need to eval these lines:

Code: Select all

eval("one[i] = document.myform.mie_irent"+i+".value;");
(#10850)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

set a counter and call the function the number of MAX times you set. When the threshold has been met, stop calling it:

ex:

Code: Select all

var cnt = 0;
var max = 5;
function stuff()
{
  ...
  cnt++;
}
if(cnt != max)
  stuff()
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I tired it this way but it still does not work. gives me a js error saying object expected

Code: Select all

var i=1;
for (i=1; i<=15; i++)  {
  startCalc(i);
	stopCalc(i);
}

function startCalc(i){
  interval[i] = setInterval("calc("+i+")",1);
}
function calc(i){
   one[i] = document.myform.mie_irent[i].value;
   two[i] = document.myform.mie_impfee[i].value;
   three[i] = document.myform.mie_idcredits[i].value;
   four[i] = document.myform.mie_irmbs[i].value;
   five[i] = document.myform.mie_ioinc[i].value;
   document.myform.mie_itotal[i].value = (one[i] * 1) + (two[i] * 1) + (three[i] * 1) + (four[i] * 1) + (five[i] * 1); 
}
function stopCalc(i){
  clearInterval(interval[i]);
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

are you sure the form fields are returning values?

you might need to parseInt() or parseFloat() them so JS doesn't think they're strings...
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I am not strong with my js syntax that is why i am requesting someone to give me the correct way to write my function. The functions works perfectly when I use it with a single form, it is only when I use it in the array that the issue happens

Thanks in advance
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ahh...I see the problem.

you're trying to dynamically name the function.

remove the from the function name. Just pass the as the only argument to the function.

ex:

Code: Select all

for(i=0;i<10;i++)
{
    someFunction(i);
}
someFunction(num)
{
   alert(num);
}
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Based on your suggestion I modified it to this. Let me know if this will work

Code: Select all

var i=1;
for (i=1; i<=15; i++)  {
  startCalc(i);
	stopCalc(i);
}

function startCalc(num){
  interval = setInterval("calc(num)",1);
}
function calc(num){
alert('Your password must be at least ' + num + ' characters long. Try again.');
   one = document.myform.mie_irent"+num+".value;
   two = document.myform.mie_impfee"+num+".value;
   three = document.myform.mie_idcredits"+num+".value;
   four = document.myform.mie_irmbs"+num+".value;
   five = document.myform.mie_ioinc"+num+".value;
   document.myform.mie_itotal"+num+".value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1); 
}
function stopCalc(num){
  clearInterval(interval);
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

sulen wrote:... Let me know if this will work
it works the other way around...you tell me if it works :P
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

No it does not work. This is the lastest version of what I am trying but still tro no effect. Thats why I asked if I was making a mistake with the syntax or soemthing. Thanks

Code: Select all

<script Language="JavaScript" >
function startCalc(num){
  interval = setInterval("calc("+num+")",1);
}
function calc(no){
   one = document.myform.mie_irent"+no+".value;
   two = document.myform.mie_impfee"+no+".value;
   three = document.myform.mie_idcredits"+no+".value;
   four = document.myform.mie_irmbs"+no+".value;
   five = document.myform.mie_ioinc"+no+".value;
   document.myform.mie_itotal"+no+".value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1); 
}
function stopCalc(){
  clearInterval(interval);
}
</script>

<form name='myform'>
<? for ($j=1;$j<=15;$j++) { ?>
<input name="mie_irent<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc(<?=$j?>);" onBlur="stopCalc();" />
<input name="mie_impfee<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc(<?=$j?>);" onBlur="stopCalc();" />
<input name="mie_idcredits<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc(<?=$j?>);" onBlur="stopCalc();" />
<input name="mie_irmbs<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc(<?=$j?>);" onBlur="stopCalc();" />
<input name="mie_ioinc<?=$j?>" type="text" class="txtbox" size="20" maxlength="20" value="" onFocus="startCalc(<?=$j?>);" onBlur="stopCalc();" />
TOTAL MONTHLY INCOME: $ <input name="mie_itotal<?=$j?>" type="text" class="txtbox1" size="20" />
<? } ?>
</form>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I don't think you can access keys in the form array like that...

what are you ultimately trying to do? let's start there and maybe I can help you a little better.

your functions as they are now don't make much sense...I don't see the reason to set an interval to call the same function that does the same thing then kill the interval on blur of the fields.... it's tremendous overkill with no rhyme or reason.

I also don't understand why you're multiplying the values of the fields by 1 then adding them...why not just add them?
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Do not worry I have found the solution. For those interested the function is as under

Code: Select all

function startCalc(num){
  interval = setInterval("calc("+num+")",1);
}

function calc(no){
   one   = document.myform["mie_irent"+no].value;
   two   = document.myform["mie_impfee"+no].value;
   three = document.myform["mie_idcredits"+no].value;
   four  = document.myform["mie_irmbs"+no].value;
   five  = document.myform["mie_ioinc"+no].value;
   document.myform["mie_itotal"+no].value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1);
}

function stopCalc(){
  clearInterval(interval);
}
Post Reply