The jQuery $ () selector is missing for "#<%=lstSvcName.ClientID %>" , so you get id lstSvcName instead of object .
I also changed the append statement as it does not have the correct syntax.
"#<%=lstSvcName.ClientID %>"
will be
$("#<%=lstSvcName.ClientID %>")
Your code will become
$("#<%= btnAddSvc.ClientID %>").click(function () { var svc = $("#<%= txtServiceName.ClientID %>").val(); //Its Let you know the textbox value $("#<%=lstSvcName.ClientID %>").append('<option value="'+svc+'">item '+svc+'</option>'); return false; });
EDIT [Additional Features Requested by OP for Unique Items in ListBox and Clearing TextBox]
$("#<%= btnAddSvc.ClientID %>").click(function () { var txt = $("#<%= txtServiceName.ClientID %>"); var svc = $(txt).val(); //Its Let you know the textbox value var lst = $("#<%=lstSvcName.ClientID %>"); var options = $("#<%=lstSvcName.ClientID %> option"); var alreadyExist = false; $(options).each(function () { if ($(this).val() == svc) { alert("Item alread exists"); alreadyExist = true; return; } txt.val(""); // alert($(this).val()); }); if(!alreadyExist) $(lst).append('<option value="' + svc + '">' + svc + '</option>'); return false; });
source share