dsonesuk Posted March 28, 2012 Share Posted March 28, 2012 IF! you do not want the form to submit by enter or clicking the submit button, which will cause the reloading the page. You need to return false on whatever value are entered! valid or invalid. function calcResult() {n=document.getElementById('userValue').valuen=parseFloat(n);var userValue=document.forms["hba1c"]["userValue"].value;if (userValue==null || userValue==""){alert("Παρακαλώ καραχωρήστε την τιμή της HbA1c!");}resultEl=(n*33.3-86).toFixed(1);m='mg/dl';d='Εκτιμώμενη μέση τιμή σακχάρου: ';document.getElementById('resultl').innerHTML=resultEl + " " + m;document.getElementById('table_hba1c').style.display="table";return false;} Link to comment Share on other sites More sharing options...
hariskar Posted March 28, 2012 Author Share Posted March 28, 2012 Thank you, but I want function calcResult to start by Enter (and by pressing the input type="button" which already happenes).. Link to comment Share on other sites More sharing options...
thescientist Posted March 28, 2012 Share Posted March 28, 2012 I'm not sure what the issue is now. Can you please just post your relevant HTML and JS and please tell us specifically what is happening, and what you don't want happening, and you want to happen instead? Using false like we've been saying will work, but only if you implement it correctly. Link to comment Share on other sites More sharing options...
dsonesuk Posted March 28, 2012 Share Posted March 28, 2012 You also need to use onclick="return calcResult();", and maybe onsubmit="return calcResult();" in the form element itself. Link to comment Share on other sites More sharing options...
hariskar Posted March 28, 2012 Author Share Posted March 28, 2012 Thank you!I would like to have by writing a number and hitting Enter the same behaviour as witing the number and pressing Input type="button" which is onclick="calcResult();How can I hit Enter and start "function calcResult();" (instead of reloading the page like it happenes now)? You also need to use onclick="return calcResult();", and maybe onsubmit="return calcResult();" in the form element itself. I use onclick="return calcResult();" for the button and it works, but I cannot use it for <input type="text" id="userValue" size="15"> <form id=hba1c action="" method="post" name="HbA1c_form">Hba1c% <input type="text" id="userValue" size="15"><input type="button" value="Calculate;" onclick="calcResult();"></form> <table id="table_hba1c" style="display:none;"><tr><td width="460"></td><td width="200">Αποτέλεσμα</td></tr><tr><td>Εκτιμώμενη μέση τιμή σακχάρου</td><td id="resultl"></td></tr></table> <script type="text/javascript">function calcResult() {n=document.getElementById('userValue').valuen=parseFloat(n);var userValue=document.forms["hba1c"]["userValue"].value; if (userValue==null || userValue==""){alert("Παρακαλώ καραχωρήστε την τιμή της HbA1c!");return false;} resultEl=(n*33.3-86).toFixed(1);m='mg/dl';d='Εκτιμώμενη μέση τιμή σακχάρου: ';document.getElementById('resultl').innerHTML=resultEl + " " + m;document.getElementById('table_hba1c').style.display="table"; return false;}</script> Link to comment Share on other sites More sharing options...
dsonesuk Posted March 28, 2012 Share Posted March 28, 2012 No, No the FORM element <form id=hba1c action="" method="post" name="HbA1c_form" onsubmit="return calcResult();"> this especially will catch both enter press or submission by clicking submit button. Link to comment Share on other sites More sharing options...
hariskar Posted March 28, 2012 Author Share Posted March 28, 2012 Thank you, it worked!I had tried onsubmit, but at <input type="text" id="userValue" size="15"> with no result..Thank you again! Link to comment Share on other sites More sharing options...
hariskar Posted April 7, 2012 Author Share Posted April 7, 2012 At the bottom of page http://www.mikroviol.../articles/hba1c you can see the formula!Thank you fro helping me!! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.