Muhammad Danish
Muhammad Danish

Reputation: 109

To get the id of the control in javascript which is created by asp.net at runtime

How to get the id of control which is created at run time in asp.net. I want to get that Id in javascript.

the actual id which I am setting is ctus_txtFirstName

at run-time the id is MainContent_ctus_txtfirstName

Upvotes: 1

Views: 2921

Answers (1)

Alex
Alex

Reputation: 35407

Use the server control's ClientID property. It'll output the correct ID, containing the fully qualified Naming Container.

<script>
    var ctrl = document.getElementById('<%= txtFirstName.ClientID %>');

    console.log(ctrl.value);
<script>

This is only applicable if you're using server controls. Plain markup will not have a Naming Container, if the runat="server" attribute is omitted.

Upvotes: 3

Related Questions