9

the onkeyup method is supposedly not defined, however, the method is auto-recommended to me by my ide. When I view the error in chrome dev tools I get the error Uncaught TypeError: Object [object Object] has no method 'onkeyup'. I am using the latest version of jQuery. Here is my code:

    <!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("button").click(function(){
                var str = document.getElementById('txt1').value;
                $.post("addName.php", {q: str});
            });
            $("input").onkeyup(function(){
                var str = document.getElementById('txt1').value;
                $.post("gethint.php", {q: str});
            });
        });
    </script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action="">
    First name: <input type="text" id="txt1"/>
</form>
<p>Suggestions: <span id="txtHint"></span></p>
<button> Add Name</button>

</body>
</html>
rlemon
  • 17,518
  • 14
  • 92
  • 123
Julian
  • 325
  • 1
  • 3
  • 10

4 Answers4

17
$("input").on('keyup', function(){
    var str = document.getElementById('txt1').value;
    $.post("gethint.php", {q: str});
});

or

$("input").keyup( function(){
    var str = document.getElementById('txt1').value;
    $.post("gethint.php", {q: str});
});

jQuery has no method name onkeyup

Read more about .keyup() and .on()

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • Thank you. This solved the problem. I am using $.post correctly, right? Because now that the event is being triggered, I don't think the script is still ever being run :/ I know this because the echoes I have at the top of the file are never output. – Julian Jun 18 '12 at 15:40
  • Just wondering why you would use: var str = document.getElementById('txt1').value; instead of: var str = $('#txt1').val(); Since you are using Jquery – Sunjalo Jan 24 '13 at 08:50
1

The method is:

$("input").keyup(function() {
})
cloakedninjas
  • 4,007
  • 2
  • 31
  • 45
0

instead of onkeyup use keyup

 $("input").keyup(function(){
                var str = document.getElementById('txt1').value;
                $.post("gethint.php", {q: str});
            });
0

The method onkeyup doesn't exist, the error message clearly says "onkeyup method not defined". It should be just keyup.

$("input").keyup(function() {
    //Your code
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124