Advertisement
richarduie

getMaxInput.html

Apr 3rd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <script type="text/JavaScript">
  5. function get(eid) {return document.getElementById(eid);};
  6. function getMax( idPrefix, cnt ){
  7. for (var i = 0; i < cnt; i++) {
  8. // read value of input with id of [idPrefix + i] and
  9. // convert to numeric, if possible
  10. var num = parseFloat(get(idPrefix + i).value);
  11. // if conversion fails, advise user and exit immediately
  12. if (isNaN(num)) {
  13. alert('all inputs must be numeric');
  14. return;
  15. }
  16. //...otherwise, if first iteration, keep number - for
  17. // later iterations keep biggest
  18. if (0 == i) max = num;
  19. else max = (num < max)?max:num;
  20. };
  21. get( 'max' ).value = max;
  22. }
  23. </script>
  24. </head>
  25.  
  26. <body>
  27. <form>
  28. <p>
  29. Instructions:<br />
  30. Enter numbers into fields and click "go" button.<br />
  31. The maximum value of your inputs will appear below.<br /><br />
  32. Input values:<br />
  33. <!--
  34. inputs all have ids like ["in" + integer] - "go" button sends
  35. prefix "in" and total count of inputs to getMax() function
  36. -->
  37. <input id="in0" type="text" /> = Input 1<br />
  38. <input id="in1" type="text" /> = Input 2<br />
  39. <input id="in2" type="text" /> = Input 3<br />
  40. <input id="in3" type="text" /> = Input 4<br />
  41. <input id="in4" type="text" /> = Input 5<br />
  42. <input type="button" value="go" onclick="getMax( 'in', 5 )" /><br />
  43. Output value:<br />
  44. Maximum: <input type="text" id="max" />
  45. <p>
  46. </form>
  47. </body>
  48.  
  49. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement