0

All I have to do is to show a number in a textbox and a button which add 10 every time I press it, here's my code (it doesn't work).

<head>
    <script type="text/javascript">
    var n=parseInt(ocument.forms["formNum"]["numero"].value);
    document.getElementById("numero").value=n;

    function sumar() {
        n=document.forms["formNum"]["numero"].value+10;
        document.forms["formNum"]["numero"].value=n;
    }

    function inicializar() {
        n=document.forms["formNum"]["numero"].value=0;
    }
    </script>
</head>

<body>
    <form name="formNum">
        <p>
            <input type="text" size="10" name="numero" id="numero" />
        </p>

        <p>
            <input type="button" name="sumar" value="Sumar" onclick="sumar()" />
            <input type="button" name="inicializar" value="Iniciar a 0" onclick="inicializar()" />
        </p>
    </form>
</body>

4 Answers 4

4
<body>
    <script type="text/javascript">
       function sumar(){
           document.getElementById("numero").value = parseInt(document.getElementById("numero").value)+10;
       }

       function inicializar(){
           document.getElementById("numero").value=0;
       }
    </script>

    <form name="formNum">
        <p>
            <input type="text" size="10" name="numero" id="numero" value="0" />
        </p>

        <p>
            <input type="button" value="Sumar" onclick="sumar()" />
            <input type="button" value="Iniciar a 0" onclick="inicializar()" />
        </p>
     </form>
</body>

Five suggestions.

  1. It is always better to give unique ids to your html elements.
  2. Never name your HTML elements and javascript functions the same.
  3. If you want to access the html element from javascript, if you know the id, use getElementById.
  4. Use Firebug or Developer tools from the browser, to debug.
  5. If you want to access your elements with the hierarchy, use elements in the form like this document.forms["formNum"].elements["numero"].value. Check this example

Demo: http://jsfiddle.net/DPJCR/

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please explain what you tried and what didn't work?
Finally solved, it didn't work because i gave the samen "name" and "id" to the buttons...
0

This code should work:

<input type="text" id="mytext">

<script type="text/javascript">
   var elem = document.getElementById("mytext");
   elem.value = "My default value";
</script>

See: Set the value of an input field

Maybe you are getting an exception from the parseInt that prevents the value from changing.

Comments

-1

If it is an option to use jQuery, try this:

function sumar(){
    $("#numero").attr("value", parseInt($("#numero").attr("value"), 10)+10);
}

Comments

-1

Try this this will help you

var count=10;
$('#btnSumar').click(function(){
              if($('#numero').val()=='')
              {
                  $('#numero').val(count);
              }else

           $('#numero').val(eval($('#numero').val())+count);    

                     });
$('#btnInc').click(function(){
    $('#numero').val('');});

Fiddle here

3 Comments

Any reason to use eval here?
evali used to retrive numaric value from textbox rather than string
That's what parseFloat()/parseInt() for. Other problems I see here: unsolicited use of jQuery and not cached selectors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.