mercredi 20 décembre 2017

Based on HTML forms

I have been trying to create a form in which when we click the button with respective values from 1 to 10 then the selected number gets written in the input "Name". The code is as follow:

            <!DOCTYPE html>
            <html>
            <head>
            <script>
            function validateForm() {
                var x=document.forms["myForm"]["fname"].value;
                if(isNaN(x) || x<1 || x>10)
                {
                    alert("Number should be between 1 to 10");
                    return false;
                }
            }
            function click(theid)
            {
                var text=theid.toString();
                var val=document.forms["myForm"][text].value;
                val=Number(val);
                document.forms["myForm"]["fname"].value=val;
            }
            </script>
            </head>
            <body>

            <form name="myForm" onsubmit="return validateForm()" method="post">
            Name: <input type="text" name="fname" id="fname"><input type="submit" value="Submit">
            <br><br>
            <input type="button" value="1" id="one" name="one" onclick="click('one')">        
            <input type="button" value="2" id="two" onclick="click('two')">     
            <input type="button" value="3" id="three" onclick="click('three')">     
            <input type="button" value="4" id="four" onclick="click('four')">     
            <input type="button" value="5" id="five" onclick="click('five')">    
            <input type="button" value="6" id="six" onclick="click('six')">     
            <input type="button" value="7" id="seven" onclick="click('seven')">     
            <input type="button" value="8" id="eigth" onclick="click('eigth')">     
            <input type="button" value="9" id="nine" onclick="click('nine')">     
            <input type="button" value="10" id="ten" onclick="click('ten')">     
            </form>
            </body>
            </html>

Number clicked is not being written in the input




Aucun commentaire:

Enregistrer un commentaire