1

I have a JS function to make textbox operation. When I sent textbox client ID, I am receiving this syntax error:

JavaScript runtime error: Syntax error, unrecognized expression: #<%=txtEposta.ClientID%>

How can I fix this error? I am very new to JavaScript. Whatever I tried I cannot find a solution. Please help.

function SearchText(clientID) {
    console.log("#" + clientID);
    var availableTags = ["gmail.com", "hotmail.com", "mynet.com", "yahoo.com", "outlook.com", "windowslive.com"];
    $("#"+clientID).autocomplete({
        source: availableTags,
        matchCase: false,
        focus: function (event, ui) {
            var oldValue = $("#" + clientID).val();
            var value = oldValue + ui.item.value;
            event.preventDefault();
            $("#" + clientID).val(value);
        },

        select: function (event, ui) {
            var oldValue = $("#" + clientID).val();
            var value = oldValue + ui.item.value;
            $("#" + clientID).val(oldValue);
            event.preventDefault();
        },

        minLength: 0
    });
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="TextBoxControl.WebUserControl1" %>

<link href="/Script/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="jquery-1.8.3.js"></script>
    <script type="text/javascript" src="jquery-ui-1.8.24.js"></script>>
    <script type="text/javascript" src="../common.js"></script>>
<div>   
<asp:Textbox runat="server" ID="txtEposta" MaxLength="100" Width="250px" onKeyPress="javascript:SearchText('<%=txtEposta.ClientID%>');"                  
    ControlType="AlpfaNumericAndSymbols" AllowSpaces="False" Visible="true"></asp:Textbox>

</div>

2 Answers 2

1

You could simply replace the txtEposta.ClientID with this.

<asp:Textbox runat="server" ID="txtEposta" onKeyPress="javascript:SearchText(this);"

<script>
    function SearchText(element) {
        alert(element.id);
    }
</script>

If you really want to use ClientID you will have to add it programatically

<asp:TextBox runat="server" ID="txtEposta"></asp:TextBox>

<script>
    function SearchText(element) {
        alert(element);
    }
</script>

and then in Page_Load

txtEposta.Attributes.Add("onKeyPress", string.Format("javascript:SearchText('{0}');", txtEposta.ClientID));
Sign up to request clarification or add additional context in comments.

Comments

0

Set the property ClientIDMode in you Textbox to Static. Thes will make the ClientID be the same as the id on server side. Don't forget to change the onKeyPress argument.

<asp:Textbox runat="server" ID="txtEposta" ClientIDMode="Statis" MaxLength="100" Width="250px" onKeyPress="javascript:SearchText('txtEposta');"                  
ControlType="AlpfaNumericAndSymbols" AllowSpaces="False" Visible="true"></asp:Textbox>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.