Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script type="text/JavaScript">
- function get(eid) {return document.getElementById(eid);};
- function getMax( idPrefix, cnt ){
- for (var i = 0; i < cnt; i++) {
- // read value of input with id of [idPrefix + i] and
- // convert to numeric, if possible
- var num = parseFloat(get(idPrefix + i).value);
- // if conversion fails, advise user and exit immediately
- if (isNaN(num)) {
- alert('all inputs must be numeric');
- return;
- }
- //...otherwise, if first iteration, keep number - for
- // later iterations keep biggest
- if (0 == i) max = num;
- else max = (num < max)?max:num;
- };
- get( 'max' ).value = max;
- }
- </script>
- </head>
- <body>
- <form>
- <p>
- Instructions:<br />
- Enter numbers into fields and click "go" button.<br />
- The maximum value of your inputs will appear below.<br /><br />
- Input values:<br />
- <!--
- inputs all have ids like ["in" + integer] - "go" button sends
- prefix "in" and total count of inputs to getMax() function
- -->
- <input id="in0" type="text" /> = Input 1<br />
- <input id="in1" type="text" /> = Input 2<br />
- <input id="in2" type="text" /> = Input 3<br />
- <input id="in3" type="text" /> = Input 4<br />
- <input id="in4" type="text" /> = Input 5<br />
- <input type="button" value="go" onclick="getMax( 'in', 5 )" /><br />
- Output value:<br />
- Maximum: <input type="text" id="max" />
- <p>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement