Jump to content

JavaScript problem


kickoutbettman

Recommended Posts

Hi all, I don't know if I can be helped here with this issue.

 

Basicaly, I have a table with 18 inputs and I put numbers in. I have another field called "total".

Everytime I enter a number an onchange() action calls a JS function which adds this number to the actual total field.

 

It works fine as far as I don't go back and EDIT a number. If I go back and change the value it does add the new value to the total but doesn't subtract the old value out of it.

 

Is there anyone that can give me a hint on this. Even if you don't know JS, maybe there's a logic you can think of.

 

Thank you all for your time.

???

Link to comment
Share on other sites

The only way I can think of doing this would be to store the value of the field when the user clicks on it by using the onfocus() event on the field. Then in your onchange() js you can compare the old value with the new value and add/subtract the difference from the total.

Link to comment
Share on other sites

This is a golf stats keeper by the way.

 

***HTML***

I have 18 of thoses all named total_shot_# from 1 to 18

<input class="gameData" name="total_shot_1" type="text" id="total_shot_1" size="2" maxlength="2"  onChange="javascript:countTotal(total_shot_1.value,1);">

Parameters sent to JS :

total_shot_1.value(is the actual value of the field)

1(hole number)

 

Now the little function in JS

<script>
function countTotal(s,h)
{	
	var y = Number(s);
	var z = Number(document.getElementById("total").value);
	var total = y+z;
    document.getElementById("total").value = total;
}

</script>

 

The number are added to the total field :

<input class="gameData" name="total" type="text" id="total" size="2" maxlength="2" value="0">

 

Thanks

Link to comment
Share on other sites

function countTotal() // Note no variables

      var total = 0;

      var fieldName;

     

      for(i=0;i<19;i++)

      {

     

      fieldName = 'total_shot_'+i;

      if (!isNaN(Number(document.getElementById(fieldName).value)))

      {

      total=total+Number(document.getElementById(fieldName).value);

      }

     

      }

     

      document.getElementById("total").value = total;

}

Link to comment
Share on other sites

Thanks for your quick reply.

 

Unfortunatly it is not working.

Total value field is not getting updated.

 

Any idea why.

 

The total field name is ok.  document.getElementById("total").value

 

If I remove the loop and I force the total value to be a number (ex: total=5;) my field is getting updated with the value of 5.

 

But if I put back the loop and leave the total=5; it's not updating.

So if the function hits the loop the field is not getting updated.

 

???

Link to comment
Share on other sites

The loop should start with 1 - sorry

 

see code below - have tested and works

 

<script>

function countTotal() // Note no variables
{   
      var total = 0;
      var fieldName;
     
      for(i=1;i<19;i++)
      {
     
         fieldName = 'total_shot_'+i;
         if (!isNaN(Number(document.getElementById(fieldName).value)))
         {
            total=total+Number(document.getElementById(fieldName).value);
         }
        
      }
     
      document.getElementById("total").value = total;
}

</script>


<input class="gameData" name="total_shot_1" type="text" id="total_shot_1" size="2" maxlength="2"  onChange="javascript:countTotal();">
<input class="gameData" name="total_shot_2" type="text" id="total_shot_2" size="2" maxlength="2"  onChange="javascript:countTotal();">
<input class="gameData" name="total_shot_3" type="text" id="total_shot_3" size="2" maxlength="2"  onChange="javascript:countTotal();">
<input class="gameData" name="total_shot_4" type="text" id="total_shot_4" size="2" maxlength="2"  onChange="javascript:countTotal();">

etc up to 18

<input class="gameData" name="total" type="text" id="total" size="2" maxlength="2" value="0">

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.