javascript - Jquery Autocomplete _renderItem does not work when Function is called a second time -
i wanted generate 2 input fields autocomplete generates icons. used _renderitem
realised, - calling autocomplete using single class multiple input fields - second field not call _renderitem
function:
see here:
var $project = $('.project'); var projects = [{ value: "jquery", label: "jquery", desc: "the write less, more, javascript library", icon: "jquery_32x32.png" }, { value: "jquery-ui", label: "jquery ui", desc: "the official user interface library jquery", icon: "jqueryui_32x32.png" }, { value: "sizzlejs", label: "sizzle js", desc: "a pure-javascript css selector engine", icon: "sizzlejs_32x32.png" } ]; $project.autocomplete({ minlength: 0, source: projects, focus: function(event, ui) { this.val(ui.item.label); return false; } }); $project.data("ui-autocomplete")._renderitem = function(ul, item) { var $li = $('<li>'), $img = $('<img>'); $img.attr({ src: 'https://jqueryui.com/resources/demos/autocomplete/images/' + item.icon, alt: item.label }); $li.attr('data-value', item.label); $li.append('<a href="#">'); $li.find('a').append($img).append(item.label); return $li.appendto(ul); };
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> <link href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script> <div id="project-label">this 1 works:</div> <input class="project"> <br /> <br /> <br /> <br /> <div id="project-label">this 1 not show images in list:</div> <input class="project">
how comes in second autocomplete input-field images not shown. how can working. since in website using php generate fields automatically , have 1 autocompletefunction overall, calling autocomplete via class. there possibility? helping.
just wrap each
called each item in collection.
iterate on jquery object, executing function each matched element.
var $project = $('.project'); var projects = [{ value: "jquery", label: "jquery", desc: "the write less, more, javascript library", icon: "jquery_32x32.png" }, { value: "jquery-ui", label: "jquery ui", desc: "the official user interface library jquery", icon: "jqueryui_32x32.png" }, { value: "sizzlejs", label: "sizzle js", desc: "a pure-javascript css selector engine", icon: "sizzlejs_32x32.png" } ]; $project.autocomplete({ minlength: 0, source: projects, focus: function(event, ui) { this.val(ui.item.label); return false; } }); $project.each(function() { var $proj = $(this); $proj.data("ui-autocomplete")._renderitem = function(ul, item) { var $li = $('<li>'), $img = $('<img>'); $img.attr({ src: 'https://jqueryui.com/resources/demos/autocomplete/images/' + item.icon, alt: item.label }); $li.attr('data-value', item.label); $li.append('<a href="#">'); $li.find('a').append($img).append(item.label); return $li.appendto(ul); }; });
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> <link href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script> <div id="project-label">this 1 works:</div> <input class="project"> <br /> <br /> <br /> <br /> <div id="project-label">this 1 not show images in list:</div> <input class="project">
Comments
Post a Comment