Leon Posted October 2, 2015 Share Posted October 2, 2015 <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Converting Centimeter to Feet Centimeter to Inch</title> </head> <body> <h1>This method will change metric to standard.</h1> <p>Centimeter</p> <form action="" name="cvrtcm2ft2in()"> <input type="text" name="cm" value="0" onchange="cvrtcm2ft2in()"/><input onclick="Submit1" type="submit" value="submit" /></form> <form action="" name="cvrtcm2ft2in()"> <p>Feet</p> <input type="text" name="feet" value="0" onchange="cvrtcm2ft2in()"/></form> <form action="" name="cvrtcm2ft2in()"> <p>Inch</p> <input type="text" name="inch"value="0" onchange="cvrtcm2ft2in()"/></form> <script type="text/javascript"> function cvrtcm2ft2in(){ var cm=parseInt(document.cncm2ft.cm.value); if (cm>0){ var ft= x/30.58; document.cncm2ft.feet.value= feet; var figs = "The CM representation of " + x + " is " + cm +"<br>"; document.getElementById("result").innerHTML = figs; } So far im trying get cm convert to feet, but i dont know why when i input a value and nothing come out I need a solution asap. I have found a few online but i dont want to copy , i want to write my own. Please help Link to comment Share on other sites More sharing options...
dsonesuk Posted October 2, 2015 Share Posted October 2, 2015 (edited) 1) You just need a single parent <form>...</form> to wrap all inputs 2) You you don't have a method to prevent form submission, when the submit button is pressed it will reload and all content will be lost each time. 3) The onclick used in submit should be applied to form element using onsubmit="Submit1" 4) what does 'Submit1', 'cncm2ft' in document.cncm2ft.feet.value= feet, 'result', and 'x' refer to? it feels like this is just part of the code, difficult to help fully in this situation. 5) make sure to place a space between each attribute and value, this 'name="inch"value="0" ' should be 'name="inch" value="0" ' 6) missing closing script tag, but probably because this is just part of code and not the full code, 7) wrap code using code button '<>' (in menu) or manually ['code']' ...your code here... ['/code'] without quotes ' to make it easier to read. Edited October 2, 2015 by dsonesuk 1 Link to comment Share on other sites More sharing options...
davej Posted October 2, 2015 Share Posted October 2, 2015 Dsonesuk has covered the problems. I would add that you really should always include a doctype and that a form isn't needed if you aren't going to submit values to a server. You should probably learn to use document.getElementById(). http://www.w3schools.com/tags/tag_doctype.asp 1 Link to comment Share on other sites More sharing options...
Leon Posted October 2, 2015 Author Share Posted October 2, 2015 1) You just need a single parent <form>...</form> to wrap all inputs 2) You you don't have a method to prevent form submission, when the submit button is pressed it will reload and all content will be lost each time. 3) The onclick used in submit should be applied to form element using onsubmit="Submit1" 4) what does 'Submit1', 'cncm2ft' in document.cncm2ft.feet.value= feet, 'result', and 'x' refer to? it feels like this is just part of the code, difficult to help fully in this situation. 5) make sure to place a space between each attribute and value, this 'name="inch"value="0" ' should be 'name="inch" value="0" ' 6) missing closing script tag, but probably because this is just part of code and not the full code, 7) wrap code using code button '<>' (in menu) or manually ['code']' ...your code here... ['/code'] without quotes ' to make it easier to read. but when i use a single <form>...</form> i cant not initialized feet or inch in (document.cncm2ft.feet.value) or ( document.cncm2ft.inch.value) Link to comment Share on other sites More sharing options...
Ingolme Posted October 3, 2015 Share Posted October 3, 2015 You need to access elements using other DOM methods, such as getElementsByTagName(), getElementById(), parentNode, childNodes and the rest. Read the HTML DOM tutorial here: http://www.w3schools.com/js/js_htmldom.asp Here's a simple example: <input id="centimeters">var centimeters = Number(document.getElementById("centimeters").value);alert(centimeters); 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted October 3, 2015 Share Posted October 3, 2015 Again! Since there is no element with 'cncm2ft', JavaScript 'x' and ‘Submit1' to refer to i'm surprised to find it work at all. 1 Link to comment Share on other sites More sharing options...
dsonesuk Posted October 3, 2015 Share Posted October 3, 2015 Use form elements collection instead http://www.w3schools.com/jsref/coll_form_elements.asp much more easier, on onchange event call function, use for loop to loop through each input except submit ( if required? ) to retrive each input value and make calculation. 1 Link to comment Share on other sites More sharing options...
Leon Posted October 3, 2015 Author Share Posted October 3, 2015 1) You just need a single parent <form>...</form> to wrap all inputs 2) You you don't have a method to prevent form submission, when the submit button is pressed it will reload and all content will be lost each time. 3) The onclick used in submit should be applied to form element using onsubmit="Submit1" 4) what does 'Submit1', 'cncm2ft' in document.cncm2ft.feet.value= feet, 'result', and 'x' refer to? it feels like this is just part of the code, difficult to help fully in this situation. 5) make sure to place a space between each attribute and value, this 'name="inch"value="0" ' should be 'name="inch" value="0" ' 6) missing closing script tag, but probably because this is just part of code and not the full code, 7) wrap code using code button '<>' (in menu) or manually ['code']' ...your code here... ['/code'] without quotes ' to make it easier to read. Use form elements collection instead http://www.w3schools.com/jsref/coll_form_elements.asp much more easier, on onchange event call function, use for loop to loop through each input except submit ( if required? ) to retrive each input value and make calculation. Ty so much , u helped me a lot. This is my final code: <!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 content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>Cmftin</title></head><body><h1>This method will change metric to standard.</h1><form action="" name="centimeter" >Centimeter<input type="text" name="cm" value="0" />Feet<input type="text" name="ft" value="0" />Inch<input type="text" name="in" value="0" /></form><input name="Button1" type="button" value="convert" onclick="crtcm2ftin ()" /><script type="text/javascript">function crtcm2ftin (){var cm= parseInt(document.centimeter.cm.value);if (cm > 0){var ft= (cm/30.48);document.centimeter.ft.value=ft;var inch = (cm/2.54);document.centimeter.in.value=inch; }}</script><p> </p> Link to comment Share on other sites More sharing options...
davej Posted October 3, 2015 Share Posted October 3, 2015 Final code? You don't even have closing tags... </body></html> Link to comment Share on other sites More sharing options...
Leon Posted October 4, 2015 Author Share Posted October 4, 2015 Final code? You don't even have closing tags... </body></html> LOL i got too excited that i had it works, forgot to copy the last two tags <!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 content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Cmftin</title> </head> <body> <h1>This method will change metric to standard.</h1> <form action="" name="centimeter" > Centimeter<input type="text" name="cm" value="0" /> Feet<input type="text" name="ft" value="0" /> Inch<input type="text" name="in" value="0" /> </form> <input name="Button1" type="button" value="convert" onclick="crtcm2ftin ()" /> <script type="text/javascript"> function crtcm2ftin (){ var cm= parseInt(document.centimeter.cm.value); if (cm > 0){ var ft= (cm/30.48); document.centimeter.ft.value=ft; var inch = (cm/2.54); document.centimeter.in.value=inch; } } /*Leon*/ </script> <p> </p> </body> </html> Link to comment Share on other sites More sharing options...
davej Posted October 5, 2015 Share Posted October 5, 2015 Does it make sense to use a form? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now