0

I use JavaScript and am trying to use more JQuery. Anyway I never liked the way Master Pages change the ID's of my elements. So when I use getElementById("MyElement") it fails to find the element. So I hit F5 and do a view source and then copy the new ID of the element and it works.

My Question... Is there a better way to do this?

Roto
  • 527
  • 2
  • 4
  • 16
  • 2
    yes, there is a better way, instead of `getElementById("MyElement")` do `getElementById("<%= MyElement.ClientID %>")`. it will return the dynamically generated ID – Enrique Zavaleta Aug 20 '15 at 15:50
  • Thanks; just what I was looking for. It uses that old classic asp syntax. – Roto Aug 20 '15 at 15:56

1 Answers1

0

Set the ClientIDMode and ClientID of your server control, like this:

<asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static" ClientID="MyTextBox" />

Now you can reference the textbox in jQuery or plain old JavaScript via the MyTextBox ID selector.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80