orshachar
orshachar

Reputation: 5037

How to set HTML element ID at client side

We are trying to write a form at ASP.net using visual studio. We want to be able to control the final client side ID of elements so we can refer to them in the jquery code easily.

The regular 'id' attribute at ASP.net gets an odd prefix that we cannot control at the client side.

How can we fully control the final client side ID of an element?

Thank you!

IE:

<asp:TextBox ID="TBICDCode6"  runat="server" Width="240">

I want to be able to refer to the text box using

$("#TBICDCode6");

Upvotes: 0

Views: 3286

Answers (3)

codeandcloud
codeandcloud

Reputation: 55210

If you are using ASP.NET 4.0, you can do this by setting Control.ClientIDMode to static

<asp:TextBox ID="TBICDCode6"  runat="server" 
        ClientIDMode="Static"
        Width="240">
</asp:TextBox>

Or, if you are using an older version of ASP.NET, try this.

$("#" + '<%= TBICDCode6.ClientID %>')

Upvotes: 1

J. Ed
J. Ed

Reputation: 6742

it's a well known fact that asp.net does nasty things to ids. it's supposed to be better in asp.net 4.0, but I haven't tried it yet.
try reading 'Referencing Server Controls in Client Script' here.
Alternatively, you can bypass the whole thing completly by using another attribute (such as name).
your selectors will then be $("[name=someId]"), instead of $("#someId")

Upvotes: 1

davecoulter
davecoulter

Reputation: 1826

Try:

$("#<%= TBICDCode6.ClientID %>");

Upvotes: 1

Related Questions