1

I have a user-control in which I have a textbox. I am using jQuery auto-fill in this text-box.

When I use this user-control on a page multiple times, it doesn't work. I know its because of repeat ids of textbox but I am not getting how to solve this problem. Help me please .

Here is my UserContol

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Classes.ascx.cs" %>
<asp:TextBox ID="txtClass" placeholder="search Class" type="text" runat="server" EnableViewState="true"
    ClientIDMode="Static" Width="250px" OnTextChanged="txtClass_TextChanged"></asp:TextBox>


<script language="javascript">

    $(function () {

        $("#txtClass").autocomplete({
            source: function (request, response) {
                var oClass = $("#txtClass").val().replace('*', '');
                .
                .
                .
                .
                }
            });
        }

Update

here is html

    <input name="ctl00$BodyContent$txtClassasd$txtClass" id="txtClass" placeholder="search Class" type="text" style="width:250px;" />

<script language="javascript">

    $(function () {

        $("#txtClass").autocomplete({
            source: function (request, response) {
                var test = document.getElementById("txtClass").value;
                var oClass = $("#txtClass").val().replace('*', '');



<input name="ctl00$BodyContent$Classes1$txtClass" id="txtClass" placeholder="search Class" type="text" style="width:250px;" />
<script language="javascript">

    $(function () {

        $("#txtClass").autocomplete({
            source: function (request, response) {
                var oClass = $("#txtClass").val().replace('*', '');
Ahmad Abbasi
  • 1,776
  • 6
  • 29
  • 43

3 Answers3

2

You need to use ClientID for server controls

 $("#<%=txtClass.clientID%>").autocomplete(

also remove ClientIDMode="Static" from the txtClass control

Michael B.
  • 2,798
  • 1
  • 14
  • 18
0

If you look at the html that is emitted to the browser, I'm guessing there is not actually a textbox with ID of 'txtClass'. You should be doing something like $("#<%= txtClass.ClientID %>")...

Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
0

Use ID array:

How to selector ID textbox array using jquery

Or revise your selector

$("input").autocomplete({...
Community
  • 1
  • 1
Blaise
  • 21,314
  • 28
  • 108
  • 169