html - How can I run a JavaScript function by taking user input & using a button? -


<!doctype html>  <html>  <head> <title> daily recommended exercise </title>  </head>  <body>  <h2>your daily exercise schedule</h2>      <p>please select age group:</p>  <form>  0 - 5: <input type = "radio" name = "pickage" value = "age1"> <br/> 6 - 17: <input type = "radio" name = "pickage" value = "age2"> <br/> 18 - 64: <input type = "radio" name = "pickage" value = "age3"> <br/> 65 - 150: <input type = "radio" name = "pickage" value = "age4"> <br/>  <input type="button" onclick = "exerciserecommend();" value = "enter"></input>  </form>  <script type = "text/javascript"> function exerciserecommend() { var age = document.getelementsbyname("pickage");  if (age=="age1") {     alert("physical activity in infants , young children necessary     healthy growth , development. there no guidelines children @ age      though regular physical activity recommended."); } else if (age=="age2") {     alert("at age should 60 minutes or more of physical activity each day. includes, aerobic endurance , strength exercises."); } else if (age=="age3") {     alert("at age should doing 2 hours , thirty minutes or more of moderate aerobic endurance , strength exercises activity every week or 1 hour fifteen minutes of intense aerobic endurance , strength exercises activity or mix of two."); } else if (age=="age4") {     alert("at age should exercising 2-3 hours week. recommended should doing mild endurance , strength activities."); } }  </script>   </body>  </html> 

what wrong code? whenever press button nothing happens!! have tried again , again reason not finding user input , outputting alert values! please help!

shashank correct best practice attach event listener through js itself, in case i'll assume you're learning language , want know what's , how works.

so, let's take @ age variable. if console.log(age) after define it, return node list of of elements name "pickage". want specific 1 of those, checked one.

//  list of select-able ages var allages = document.getelementsbyname("pickage"); //  define variable hold our selected age var age;  //  iterate through of select-able ages (i = 0; < allages.length; i++) {     //  if selected age checked, set "age" variable     if (allages[i].checked === true) {         //  grab value here because that's check later         age = allages[i].value;     } } 

that should give correct result work if < alert. might want add else statement @ end in case user doesn't select age, though.

just make sure know, isn't best practice, efficient, or best way of doing this. quick example understand process bit basis language.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -