Add jquery ui autocomplete to dynamic textbox
Add jquery ui autocomplete to dynamic textbox
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.19/themes/cupertino/jquery-ui.css">
<div>
<ul id="ulTest" class="ul-style">
<li class="li-style">
<input id="txtTags0"/>
<button id="add0">Add</button>
<button id="del0">Del</button>
</li>
</ul>
</div>
CSS
.ul-style{
max-height:80px;
width:350px;
overflow-x:hidden;
overflow-y:auto
}
.li-style{
font-size:medium;
}
button{
width:50px;
margin-left:5px;
}
input{
width:100px;
}
.ui-autocomplete {
max-height: 100px;
overflow-y: auto;
/* prevent horizontal scrollbar */
overflow-x: hidden;
/* add padding to account for vertical scrollbar */
padding-right: 20px;
}
/* IE 6 doesn't support max-height
* we use height instead, but this forces the menu to always be this tall
*/
* html .ui-autocomplete {
height: 100px;
}
JavaScript
$(function () {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
window.count = 0;
$("button[id^='add']").live("click", null, function () {
window.count++;
var txtTagsID= "txtTags" + window.count;
var btnAddID = "add" + window.count;
var btnDelID = "del" + window.count;
var ul = $("#ulTest");
var li = $("<li class='li-style'></li>")
.append($("<input id='" + txtTagsID+ "' />"
+ "<button id='" + btnAddID + "' >Add</button>"
+ "<button id='" + btnDelID + "' >Del</button>"));
li.appendTo(ul);
});
$("button[id^='del']").live("click", null, function () {
if (window.count == 0) {
alert("You should have one textbox!");
return;
}
var li = $(this).parent();
li.remove();
window.count--;
});
$("input:text[id^='txtTags']").live("focus.autocomplete", null, function () {
$(this).autocomplete({
source: availableTags,
minLength: 0,
delay: 0
});
$(this).autocomplete("search");
});
});