1

I am having the following Radio butttons in my aspx file.

<asp:RadioButton ID="rdoOption1" runat="server" GroupName="grpAnswers"/>
<asp:RadioButton ID="rdoOption2" runat="server" GroupName="grpAnswers"/>
<asp:RadioButton ID="rdoOption3" runat="server" GroupName="grpAnswers"/>
<asp:RadioButton ID="rdoOption4" runat="server" GroupName="grpAnswers"/>

I want to set the text from javascript on a button's click.
Javascript code is as follows.

document.getElementById("rdoOption1").innerHTML = "sometext";

I've also tried with Text,nodevalue instead of innerHTML, no use. Plz help.

4 Answers 4

2

You need to use ClientID in javascript instead of using the server side id. As the server side id is changed when html is generated by asp.net. You also have : instead of semi colon at the end of javascript statement. Use value instead of innerHTML as that is not for input html elements.

document.getElementById("<%= rdoOption1.ClientID %>").value = "sometext";

If you have .Net Framework 4 and above then you can use Control.ClientIDMode="static" to keep the server id on the client side.

Edit The second thing you have to take care is to make sure the html element you are trying to access is already added to DOM. You can do this by putting the script after the html elements you are trying to access. The best place would be just before the ending body tag </body>

     <script type="text/javascript>
           document.getElementById("rdoOption1").innerHTML = "sometext";
     </script>
</body>
Sign up to request clarification or add additional context in comments.

9 Comments

hi adil... i am using .net framework 4 only... the default ClientIDMode is static oly na??? even i checked in the browser. it is rendering with same id oly.. may be of different problem?? plz help
Put the script just before ending tag, are you getting alert just to make sure your script working.
i've tried with different ID,tats is of a div container ID in the same page.It's working.but not with this radio button id.the error is like htmlfile: Unknown runtime error
What about using ClientID instead of ClientIDMode?
Hai adil... i think the problem is with asp:RadioButton, because i used an asp:Label(also a server control) in the same place as the before... working fine... my doubt is can we use innerHTML for providing Text in asp:radiobutton or we need some other... plz help.. i have no hope other than u.... :)
|
1

Atlast I've found the solution for this.
Actually the asp:RadioButton is rendering in the browser as follows,

<input name="grpAnswers" id="rdoOption1" type="radio" value="rdoOption1"/>  
<label for="rdoOption1">

Note: It'll generate label only if you specify some value for Text property of the asp:RadioButton.

So ,I have changed my code as follows,

<asp:RadioButton ID="rdoOption1" runat="server" GroupName="grpAnswers" Text=" "/>

My Javascript code to set Text for that asp:RadioButton is as follows,

document.getElementById("rdoOption1").nextSibling.innerHTML = "someText";

This one worked for me.Thanks to all those who spent their valuable time in answering my question. :)

Comments

0

ASP.Net controls are server side controls and not client side controls like HTML.

Their actual ID's get generated during the run time not before hand.

So, your syntax should be some thing like:

document.getElementById("<%= rdoOption1.ClientID %>").innerHTML = "sometext";

Comments

0

Check out this post: how-to-change-the-text-of-a-radio-button.

I don't think you can change the text using innerText of a radio button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.