Hey everyone, I'm pretty new at JS (and programming in general), and am writing a pretty simple script to calculate poker odds. I'm using a radio button form for the user to select one of two options, and depending on the option the script calculates the value and should output it to the browser. However, nothing happens when the submit button is clicked. Its probably a pretty simple fix, but in my noobness I'm not seeing it. Here is the script and related code for the form: <script type = "text/javascript"> function calculateOdds() { // Variables that will be used to calculate the pot odds var numberOuts = parseInt(pokerOdds.numberOuts.value), // Number of outs percentage; // percentage chance of hitting // Determine which radio button is active and calculate if(pokerOdds.radiobutton[1].checked) percentage = numberOuts / 47; document.write("The number of outs is " + percentage + "%."); else percentage = numberOuts / 46; document.write("The number of outs is " + percentage + "%."); } </script> <form id = "pokerOdds" action = "javascript:calculateOdds()"> <p>Select which part of the poker hand that you wish to get the odds for:</p> <input type = "radio" name = "radiobutton" value = "turn" /> <label> Odds on the turn.</label> <input type = "radio" name = "radiobutton" value = "river" /> <label> Odds on the river.</label> <p>Enter the number of outs that you have:</p> <input name = "numberOuts" type = "text" /> <p><input type = "submit" name = "submit" value = "Submit" /> <input type = "reset" name = "reset" value = "Reset" /></p> </p> Any help would be appreciated, and thanks in advance!
Try this: <script> function calculateOdds() { // Variables that will be used to calculate the pot odds var numberOuts = parseInt(pokerOdds.numberOuts.value), // Number of outs percentage; // percentage chance of hitting // Determine which radio button is active and calculate if(pokerOdds.radiobutton[1].checked) percentage = numberOuts / 47; else percentage = numberOuts / 46; document.write("The number of outs is " + percentage + "%."); }</script> <form id = "pokerOdds" action = "javascript:calculateOdds()"> <p>Select which part of the poker hand that you wish to get the odds for:</p> <input type = "radio" name = "radiobutton" value = "turn" /> <label> Odds on the turn.</label> <input type = "radio" name = "radiobutton" value = "river" /> <label> Odds on the river.</label> <p>Enter the number of outs that you have:</p> <input name = "numberOuts" type = "text" /> <p><input type = "submit" name = "submit" value = "Submit"/> <input type = "reset" name = "reset" value = "Reset" /></p> </p> Code (markup): You didn't have brackets around the if block and else block. I moved the .write to a line of it's own. You might also want to have a paragraph with the id "results" and use document.getElementById("results").innerHtml = ("The number of outs is " + percentage + "%."); If you do that instead of submitting the form, they can re-enter their results without having to back up.
Hey thanks! I didn't know about that method, but it sounds like it is a lot simpler (and more user friendly). I'll try it out.
Ok, I made the changes and am still running into the same problem - nothing happens when "Submit" is pressed. Any other ideas of what might be going wrong?
I think you're right, I did catch that and fix it locally, and have uploaded the changes. However, when I view the source online the </form> tag isn't showing up. I'm going to try a different FTP program and see if I can figure out why its not updating.