Best practice for referencing an asp.net control in jQuery

I see 4 or more ways to access an asp.net control in jQuery

$ ("input [ID = $ 'txt1']"); OR $ ("# <% = txt1.ClientID%>"); OR $ ("# Txt1"); OR or access using a class

Can you indicate which one to choose in which scenario

+4
source share
3 answers

Here is an excellent discussion on this issue , in particular, on accelerating the performance of ends with a selector (see the comments for some alternative solutions too).

+3
source

I don’t like to see the embedded code, so I would never use $ ("# <% = txt1.ClientID%>");

I would probably use $ ("input [id $ = 'txt1']"); since I would not have duplicate names in my code, and matching with a part that is not a .NET add-on will be reliable enough.

+1
source

I used the endsWith selector that you included in your question. All other options require additional work on the server side.

-2
source

All Articles