kickoutbettman Posted April 24, 2009 Share Posted April 24, 2009 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. ??? Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted April 24, 2009 Share Posted April 24, 2009 some code please Quote Link to comment Share on other sites More sharing options...
radi8 Posted April 24, 2009 Share Posted April 24, 2009 Also, this is a PHP forum, not a JAVASCRIPT forum. I think we can help, but you are in the wrong place. Quote Link to comment Share on other sites More sharing options...
NightMonkey Posted April 24, 2009 Share Posted April 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted April 24, 2009 Share Posted April 24, 2009 There is a much simpler way of doing it than that - but is easier to explain when I can refer to parts of the original code - please post it. Quote Link to comment Share on other sites More sharing options...
kickoutbettman Posted April 24, 2009 Author Share Posted April 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted April 24, 2009 Share Posted April 24, 2009 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; } Quote Link to comment Share on other sites More sharing options...
kickoutbettman Posted April 24, 2009 Author Share Posted April 24, 2009 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. ??? Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted April 24, 2009 Share Posted April 24, 2009 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"> Quote Link to comment Share on other sites More sharing options...
kickoutbettman Posted April 24, 2009 Author Share Posted April 24, 2009 GREAT !!! It works perfect. Very easy solution, thank you very much you saved me a lot of time !. Cheers ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.