1

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

    <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>
Edgar P-Yan
  • 78
  • 1
  • 8
  • 4
    typo: should be `function myFunction () {` – kukkuz Aug 23 '17 at 18:42
  • 1
    thanks) Im frequently doing mistakes like this – Edgar P-Yan Aug 23 '17 at 18:47
  • @edgar-p-yan. It's nice to get into the habit of setting break-points or at least do some `console.log("inside function")` so that you can see at which stage the code if failing. The output is shown in the console (in Chrome you can access it by pressing CTRL + SHIFT + I and switching to the Console tab. There you can also see any error message, and they may indicate where/what your error is. – jonahe Aug 23 '17 at 18:49

2 Answers2

1

Very simple you have forgotten to place parantheses after myFunction.

your code should be:

 <script>
function myFunction(){
    document.getElementById('but').value = "changed";
}
</script>
Sadhon
  • 674
  • 7
  • 11
0

This way it work. Function needs parenthesis

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

    </body>
    </html>

an alternative way is

    myFunction = function () {
        document.getElementById('but').value = "changed";
    }
stefan bachert
  • 9,413
  • 4
  • 33
  • 40