twilitegxa Posted July 27, 2009 Share Posted July 27, 2009 I have a form that I have set to calculate values on the click of a button. How can I change this to calculate the values onblur so that I can make the button a submit button? Here is the form: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css" media="screen"> /*<![CDATA[*/ @import url(global.css); /*]]>*/ </style> <!-- Script for calculating Derived Values --> <script language="javascript"> function healthv() { var first,sec,third,res1; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].bodyv.value); //Take the value of second textbox and convert it to float sec=parseFloat(document.forms[0].mind.value); //third third=parseFloat(document.forms[0].soul.value); res1=(first+third)*5; //show the result in the result textbox document.forms[0].health.value=res1; } function energyv() { var sec,third,res2; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].bodyv.value); //Take the value of second textbox and convert it to float sec=parseFloat(document.forms[0].mind.value); //third third=parseFloat(document.forms[0].soul.value); res2=(sec+third)*5; //show the result in the result textbox document.forms[0].energy.value=res2; } function acv() { var first,sec,third,res3; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].bodyv.value); //Take the value of second textbox and convert it to float sec=parseFloat(document.forms[0].mind.value); //third third=parseFloat(document.forms[0].soul.value); res3=Math.floor((first+sec+third)/3); //show the result in the result textbox document.forms[0].txtresult.value=res3; } function dcv() { var first,sec,third,res4; //Take the value of first textbox and convert it to float first=parseFloat(document.forms[0].bodyv.value); //Take the value of second textbox and convert it to float sec=parseFloat(document.forms[0].mind.value); //third third=parseFloat(document.forms[0].soul.value); res4=Math.floor((first+sec+third)/3 -2); //show the result in the result textbox document.forms[0].dcv1.value=res4; } </script> <!-- End Derived Values Script --> </head> <body> <div id="main"> <form name="cal" method="post" action=""> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td><div align="left"><strong>Body:</strong></div></td> <td><input name="bodyv" type="text" id="bodyv" /></td> </tr> <tr> <td><div align="left"><strong>Mind:</strong></div></td> <td><input name="mind" type="text" id="mind"></td> </tr> <tr> <td><div align="left"><strong>Soul:</strong></div></td> <td><input name="soul" type="text" id="soul"></td> </tr> <tr> <td></td> <td> </td> </tr> <tr> <td><div align="left"><strong>Health Points:</strong></div></td> <td><input name="health" type="text" id="health"readonly="readonly"></td> </tr> <tr> <td><div align="left"><strong>Energy Points:</strong></div></td> <td><input name="energy" type="text" id="energy" readonly="readonly"></td> </tr> <tr> <td><div align="left"><strong>Attack Combat Value:</strong></div></td> <td><input name="txtresult" type="text" id="txtresult" readonly="readonly"></td> </tr> <tr> <td><div align="left"><strong>Defense Combat Value:</strong></div></td> <td><input name="dcv1" type="text" id="dcv1" readonly="readonly"></td> </tr> <input name="total_cp" type="hidden" id="total_cp" /> <tr> <td> <div align="left"> </div></td> <td><input name="calcstats" type="button" id="submit" value="Calculate Values" onclick="acv(), dcv(), healthv(), energyv()"></td> </tr> </table> </form> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/167578-solved-onblur-help/ Share on other sites More sharing options...
smerny Posted July 27, 2009 Share Posted July 27, 2009 <input type='text' name='whatever' onblur='functionname()'...etc Link to comment https://forums.phpfreaks.com/topic/167578-solved-onblur-help/#findComment-883715 Share on other sites More sharing options...
twilitegxa Posted July 27, 2009 Author Share Posted July 27, 2009 Okay, that works, but is there a way to make it not say NaN when one of the values from the calculation hasn't been entered yet? Link to comment https://forums.phpfreaks.com/topic/167578-solved-onblur-help/#findComment-883721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.