{"id":7800,"date":"2015-10-27T09:07:18","date_gmt":"2015-10-27T09:07:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/jquery-chosen-plugin-dynamically-populate-list-by-ajax-open-source-projects-harvesthq-chosen\/"},"modified":"2015-10-27T09:07:18","modified_gmt":"2015-10-27T09:07:18","slug":"jquery-chosen-plugin-dynamically-populate-list-by-ajax-open-source-projects-harvesthq-chosen","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/10\/27\/jquery-chosen-plugin-dynamically-populate-list-by-ajax-open-source-projects-harvesthq-chosen\/","title":{"rendered":"Jquery Chosen plugin &#8211; dynamically populate list by Ajax-open source projects harvesthq\/chosen"},"content":{"rendered":"<p>Im trying to build my dropdown menu using the plugin Chosen for <strong>Multiple Select<\/strong> . Here&#8217;s to behavior I&#8217;m based on:<\/p>\n<p>http:\/\/jsfiddle.net\/JfLvA\/<\/p>\n<p>So, instead of having 3 harcoded &lt; option &gt; in my select. I want this list to be the values of a json array populated by an ajax request. This will be triggered by autocomplete.<\/p>\n<p>So, if the user type &#8220;car&#8221;, im sending the letter via an ajax call, and im getting back an array like that:<\/p>\n<p>[{&#8220;id&#8221;:&#8221;2489&#8243;,&#8221;name&#8221;:&#8221;carrie&#8221;},{&#8220;id&#8221;:&#8221;2490&#8243;,&#8221;name&#8221;:&#8221;Caroline&#8221;},{&#8220;id&#8221;:&#8221;2491&#8243;,&#8221;name&#8221;:&#8221;Carole&#8221;}]<\/p>\n<p>The code:<\/p>\n<pre><code>$(function() {\n\n$(\".chzn-select\").chosen();\n$(\".chzn-select-deselect\").chosen({allow_single_deselect:true});\n\n$('.chzn-choices input').autocomplete({\n   source: function( request, response ) {\n      $.ajax({\n          url: \"\/change\/name\/autocomplete\/\"+request.term+\"\/\",\n          dataType: \"json\",\n          success: function( data ) {\n             response( $.map( data, function( item ) {\n                $('ul.chzn-results').append('<\/code><\/pre>\n<li class=\"active-result\"><code>' + item.name + '<\/code><\/li>\n<pre>');\n\n          }\n       });\n    }\n});\n<\/pre>\n<p><code>Result:<\/code><\/p>\n<p><code>I type \"car\", in the dropdown Im getting \"No result for car\" and then I have all my results, as I want.<\/code><\/p>\n<p><code><strong>1. Why I'm I getting the \"No result\" message, cause I can see in my json array and inside my list that I'm getting results.<\/strong><\/code><\/p>\n<pre><code><code> -----------------------------\n<\/code><\/code><\/pre>\n<p>When I delete &#8220;car&#8221; and enter &#8220;sam&#8221;. The results for &#8220;sam&#8221; are showing after the &#8220;car&#8221; results. (Basically, I see the result for both, instead of just having the result of my current search)<\/p>\n<p><strong>2. Im I suppose to clear the ul on keyUp?? Thought the plugin was doing that already<\/strong><\/p>\n<pre><code> -----------------------------\n<\/code><\/pre>\n<p>When I click on a name to actually select it and add it into the select, Im getting a javascript error inside the chosen.js file<\/p>\n<p>item is undefined<br \/>\n&#8220;item.selected = true;&#8221; line 732<\/p>\n<p>the link to the plugin: http:\/\/harvesthq.github.com\/chosen\/chosen\/chosen.jquery.js<\/p>\n<p>and it&#8217;s not adding anything inside the select.<\/p>\n<p><strong>3. No idea why this is happening<\/strong><\/p>\n<pre><code> -----------------------------\n<\/code><\/pre>\n<p>Do you guys have any idea on what I&#8217;m I doing something wrong? I&#8217;m completly stuck here&#8230;!<\/p>\n<p>Oh and by the way, I dont mind changing the plugin source, as it&#8217;s the only place where I&#8217;m using it&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Im trying to build my dropdown menu using the plugin Chosen for Multiple Select . Here&#8217;s to behavior I&#8217;m based on: http:\/\/jsfiddle.net\/JfLvA\/ So, instead of having 3 harcoded &lt; option &gt; in my select. I want this list to be the values of a json array populated by an ajax request. This will be triggered [&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-7800","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7800","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=7800"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7800\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}