Darey
Darey

Reputation: 497

Obtaining control Id through javascript in asp.net

I have a scenario in my asp.net application where when enter button is keyed in any of the multiline textboxs, I need to add a particular length to the existing length and show it in another control. I have written a generic javascript function to handle key-in in "pnlEnglish" panel. I can find out "Enter key" key up by checking "e.keycode ==13" , However would it be possible to know from which control I am firing the event??. I am trying to avoid adding events to text box since, I have to handle it for too many. Is there any better solution to achieve this??

<asp:Panel ID="pnlEnglish" runat="server">

 <asp:TextBox ID="txtPlot" runat="server" Rows="5" TextMode="MultiLine" Width="350px" TabIndex="12"></asp:TextBox>
 <asp:TextBox ID="txtLegacyPlot" runat="server" Rows="5" TextMode="MultiLine" Width="350px"TabIndex="12"></asp:TextBox>
 </asp:Panel>

Javascript is as below

         $('#<%=pnlEnglish.ClientID%> input:text,#<%=pnlEnglish.ClientID%> textarea').keyup(function (e) {

       if (e.keyCode == 13) {
            //Need to identify the control for which I shall add the length
                        }
            var txtLenPlot = $('#<%=txtPlot.ClientID%>').val().length;
            var txtLenLegacyPlot = $('#<%=txtLegacyPlot.ClientID%>').val().length;


            //The below function shows the length in length displaying control
            update_chars_left(500, $('#<%=txtLengthPlot.ClientID %>'), txtLenPlot);
            update_chars_left(205, $('#<%=txtLengthLegacyPlot.ClientID %>'), txtLenLegacyPlot);

        });

Upvotes: 0

Views: 107

Answers (1)

GMD
GMD

Reputation: 761

In side key up function you can get id of textbox in which enter key is pressed as below

$('#<%=pnlEnglish.ClientID%> input:text,#<%=pnlEnglish.ClientID%> textarea').keyup(function (e) {

           if (e.keyCode == 13) {
             var currTextboxId =  $(this).attr('id');
            //Your Code

});

Upvotes: 1

Related Questions