set value of input tag to another value using javascript

17,763

You misspelled onclick:

<button type="button" onclck="resetInput();">Testing</button>
                         ^

Demo: http://jsfiddle.net/83NZu/2/

Share:
17,763
darrenc
Author by

darrenc

Updated on June 14, 2022

Comments

  • darrenc
    darrenc almost 2 years

    I have an HTML input tag and I want to change the value of it when clicking on a button, all before submitting the whole form.

    I am trying to use getElementById(idOfInput).value = "value I want to set it to", but that doesn't seem to work.

    Check out the jsfiddle here: http://jsfiddle.net/dchaddock/83NZu/1/

    <input id="resetTesting" value="Testing" />
    <button type="button" onclck="resetInput();">Testing</button>
    
    function resetInput() {
        document.getElementById("resetTesting").value = "I'm reset!";
    }
    

    Thanks!

  • JAAulde
    JAAulde about 11 years
    That is not the problem. An inline event handler is the same as the body of a function. The value of onclick is not run until click, at which point resetInput has been defined (even if you clicked during the split second it wasn't defined, subsequent clicks would be fine). Your example works because you corrected the real issue of the typo'd attribute.
  • bluetoft
    bluetoft about 11 years
    You're absolutely correct. Thanks for the lesson. The problem with the original post jsfiddle was that they had framework setting as onLoad... not sure why that broke the jsfiddle, fwiw