Hitz
Hitz

Reputation: 1071

Best Practice to refer a asp.net control in jQuery

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

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

Can you guide which one to choose in what scenario

Upvotes: 4

Views: 231

Answers (3)

patmortech
patmortech

Reputation: 10219

Here's a great discussion about this topic, specifically about speeding up the performance of the endsWith selector (look in the comments too for some alternate solutions as well).

Upvotes: 3

CaffGeek
CaffGeek

Reputation: 22054

I don't like seeing inline code so I would never use $("#<%= txt1.ClientID %>");

I would probably use $("input[id$='txt1']"); since I wouldn't have duplicated names in my code and matching on the part that isn't .NET's addition will be quite reliable.

Upvotes: 1

Josh Stodola
Josh Stodola

Reputation: 82483

I've been using the endsWith selector that you have included in your question. All other options require additional leg-work on the server-side.

Upvotes: -2

Related Questions