{"id":1903,"date":"2022-08-30T15:20:24","date_gmt":"2022-08-30T15:20:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/jquery-ui-autocomplete-js-error-on-keydown-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:24","modified_gmt":"2022-08-30T15:20:24","slug":"jquery-ui-autocomplete-js-error-on-keydown-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/jquery-ui-autocomplete-js-error-on-keydown-collection-of-common-programming-errors\/","title":{"rendered":"jquery ui autocomplete js error on keydown-Collection of common programming errors"},"content":{"rendered":"<p>i&#8217;ve included the jquery ui automcomplete plugin into the following structure:<\/p>\n<pre>\n<\/pre>\n<p><code>my javascript for this input field looks like:<\/code><\/p>\n<pre><code><code> \n\nfunction addSearchFieldFunctionality() {\n\nvar availableTags = [\n                    \"ActionScript\",\n                    \"AppleScript\",\n                    \"Asp\",\n                    \"BASIC\",\n                    \"C\",\n                    \"C++\",\n                    \"Clojure\",\n                    \"COBOL\",\n                    \"ColdFusion\",\n                    \"Erlang\",\n                    \"Fortran\",\n                    \"Groovy\",\n                    \"Haskell\",\n                    \"Java\",\n                    \"JavaScript\",\n                    \"Lisp\",\n                    \"Perl\",\n                    \"PHP\",\n                    \"Python\",\n                    \"Ruby\",\n                    \"Scala\",\n                    \"Scheme\"\n                ];  \n$('.searchfield').each(function () {\n    $(this).autocomplete({\n            source: availableTags,\n            minLength: 1\n\n\n        }).data(\"autocomplete\")._renderItem = function(ul, item) {\n            \/\/console.log(item);\n            var a = $('', {\n                href: item.value,\n                text:  item.label,\n                \"class\" : \"mySearchClass\" \n\n            });\n            var b = $('', {\n                href: item.value,\n                text: \"Add\", style: \"float:right\"});\n\n\n            var $li = $('<\/code><\/code><\/pre>\n<pre>', {style:\"width:100%\"});\n              return $li.add(a).appendTo(ul);\n        };\n});\n}\n \n<\/pre>\n<p><code>I'm loading that function on document ready. for some reason, if a start typing e.g. the first three letters of a item, i get a resultlist. as soon as i push the keydown push button on the keyword, i get the following error in the chrome (latest version) console:<\/code><\/p>\n<pre><code><code>Uncaught TypeError: Cannot read property 'top' of null\na.widget.activate                                        jquery-ui.min.js:12\na.widget.move                                            jquery-ui.min.js:12\na.widget.next                                            jquery-ui.min.js:12\na.widget._move                                           jquery-ui.min.js:12\na.widget._create.element.addClass.attr.attr.bind.bind.d  jquery-ui.min.js:12\nf.event.dispatch                                         jquery-1.7.1.min.js:3\nf.event.add.h.handle.i\n<\/code><\/code><\/pre>\n<p>i&#8217;m using version 1.7.1 of jQuery and Version 1.8.12 of jquery UI<\/p>\n<p>On the demo page of jquery ui autocomplete the keydown works well.<\/p>\n<p>Any ideas what&#8217;s going wrong with my constellation?<\/p>\n<p>It doesn&#8217;t make a difference to use remote or local data.<\/p>\n<p>Best regards, Ramo<\/p>\n<ol>\n<li>\n<p>I really can make your code working. So I tried to rewrote it in a more simplier way. The problem is render functions only accept strings, not html element. So I add a listener to render the list after its generation (fired on keydown() event).<\/p>\n<p>My thought is you are doing it the wrong way.<\/p>\n<ul>\n<li>why adding another class on those items ? they have already one, so they can be styled.<\/li>\n<li>why transforming them into a nodes ? just add a click() event on them<\/li>\n<\/ul>\n<p>Could you explain your functional goal ?<\/p>\n<pre><code>\/\/ Your list of links\nvar redirectLinks = {'Ruby': '1234.com', 'Asp': '1235.com'};\nfunction redirect(url) {\n  \/\/ TODO implement window.location=url or whatever you like\n  if(redirectLinks[url] != undefined) {\n    alert('redirecting to '+url+' =&gt; '+redirectLinks[url]);\n  }\n}\n\n$('.searchfield').each(function () {\n  $(this).autocomplete(availableTags, { \n    minLength: 1,\n    change: function(event, ui) {\n      console.log('this change event doesnt seem to be fired');        \n    },\n    select: function(event, ui) {\n      console.log('this select event doesnt seem to be fired');        \n    }\n  });\n  \/\/ After the list construction\n  $(this).keyup(function(e){      \n    if (e.which == 13) { \/\/ typing enter validates the input =&gt; autocompletechange\n      console.log('validating input '+$(this).val());    \n      redirect($(this).val());\n    }  \n    var $list = $('.ac_results:first ul').find('li');\n    $list.click(function() { \/\/ adding an event on suggestion =&gt; autocompleteselect\n      console.log('clicking on suggestion '+$(this).text());    \n      redirect($(this).text());\n    });\n  }); \n\n\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:16:53. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i&#8217;ve included the jquery ui automcomplete plugin into the following structure: my javascript for this input field looks like: function addSearchFieldFunctionality() { var availableTags = [ &#8220;ActionScript&#8221;, &#8220;AppleScript&#8221;, &#8220;Asp&#8221;, &#8220;BASIC&#8221;, &#8220;C&#8221;, &#8220;C++&#8221;, &#8220;Clojure&#8221;, &#8220;COBOL&#8221;, &#8220;ColdFusion&#8221;, &#8220;Erlang&#8221;, &#8220;Fortran&#8221;, &#8220;Groovy&#8221;, &#8220;Haskell&#8221;, &#8220;Java&#8221;, &#8220;JavaScript&#8221;, &#8220;Lisp&#8221;, &#8220;Perl&#8221;, &#8220;PHP&#8221;, &#8220;Python&#8221;, &#8220;Ruby&#8221;, &#8220;Scala&#8221;, &#8220;Scheme&#8221; ]; $(&#8216;.searchfield&#8217;).each(function () { $(this).autocomplete({ source: availableTags, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1903","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1903"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1903\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}