javascriptdom-eventsonkeydownonkeypress

Change element value on onkeypress event in JavaScript


Please help me with my script. I don't understand why it does not work:

<html>
<body>
<input type="text" id="input" onkeypress="myFunction()">
<input type="button" value="Hallo" id="but">
<script>
function myFunction{
    document.getElementById('but').value = "changed";
}
</script>
</body>
</html>

Solution

  • Very simple you have forgotten to place parantheses after myFunction.

    your code should be:

     <script>
    function myFunction(){
        document.getElementById('but').value = "changed";
    }
    </script>