0

I have made a simple fiddle where I want to call a function on keyup event, but I get function not defined. What is wrong with this code: Html:

<input type="text" id="fname" onkeyup="myFunction()">

Script:

function myFunction() {
    var input = document.getElementById("fname");
    var para = document.createElement("p");
        var node = document.createTextNode("This is new.");
        para.appendChild(node);

    if (input.value < 3){
        input.appendChild(para);
    }
}
Leff
  • 1,968
  • 24
  • 97
  • 201
  • 1
    Your function is set to run "onLoad" and not wrapped in head/body, which means that when you have a keyup event, the code you have written is no longer in scope and is undefined – Icepickle Mar 01 '17 at 22:09
  • Check this answer: http://stackoverflow.com/questions/42491561/javascript-onclick-function-doesnt-execute-error-log-not-defined/42491589#42491589 – kind user Mar 01 '17 at 22:10
  • In other words, `myFunction` must be global for the `onkeyup` to be able to access it. – qxz Mar 01 '17 at 22:11
  • isn't it global already, when it is just set as function declaration? – Leff Mar 01 '17 at 22:12

0 Answers0