I am using following Method in Javascript.
Function JsChangeRBText(itemCheckBox, rbClientId)
{
var optVal = document.getElementById(rbClientId);
if(itemCheckjBox.checked)
{
optVal.innerText = 'abc';
}
else
{
optVal.innerText = 'xyz';
}
}
here rbclientId is client id of radio button. I want to change its text on a check box checked or unchecked condition. But using above code I am unable to it.Am I missing something. I also tried for innerHTML. When I am using alert to get the current text using innerText and innerHTML, both are blank. But in alert I can see rbClientId's id and value.
I am using following Method in Javascript.
Function JsChangeRBText(itemCheckBox, rbClientId)
{
var optVal = document.getElementById(rbClientId);
if(itemCheckjBox.checked)
{
optVal.innerText = 'abc';
}
else
{
optVal.innerText = 'xyz';
}
}
here rbclientId is client id of radio button. I want to change its text on a check box checked or unchecked condition. But using above code I am unable to it.Am I missing something. I also tried for innerHTML. When I am using alert to get the current text using innerText and innerHTML, both are blank. But in alert I can see rbClientId's id and value.
Share Improve this question asked Mar 8, 2013 at 6:37 PramodPramod 6675 gold badges18 silver badges36 bronze badges6 Answers
Reset to default 3function JsChangeRBText(itemCheckBox, rbClientId)
{
var optVal = document.getElementById(rbClientId);
var label = optVal.nextSibling;
if(itemCheckjBox.checked)
{
label.value = "adc";;
}
else
{
label.value = "ccc";
}
}
HTML:
<input type="radio" /><label>Option 1</label>
or
<input type="radio" /><span>Option 1</span>
JS:
var $label = $('input[type=radio]').next();
$label.text('Options');
Firstly declare radio button like this
<input type="radio" id="rb" runat="server"/>
<label for="<%=rb.ClientID%>">option 1</label>
I mean use html radio button with runat="server"
rather than asp:RadioButton
, this will ensure rendering of control as it is and then try this
function JsChangeRBText(itemCheckBox, rbClientId)
{
var optVal = document.getElementById(rbClientId);
if(itemCheckjBox.checked)
{
optVal.nextSibling.value = 'abc';
}
else
{
optVal.nextSibling.value = 'xyz';
}
}
i know that question has been resolved but posting this solution in case it is useful for other.
But here i have used Jquery
<script src="http://code.jquery./jquery-1.9.1.min.js"></script>
<script>
Function JsChangeRBText(itemCheckBox, rbClientId)
{
var optVal = document.getElementById(rbClientId);
if(itemCheckjBox.checked)
{
$("label[for='" + rbClientId + "']").text('abc');
}
else
{
$("label[for='" + rbClientId + "']").text('xyz');
}
}
</script>
Not tested but it I guess this should work. Please do reply if it is not working.
<asp:CheckBox
ID="CheckBox1"
runat="server"
Text="Set Radio Checked"
OnCheckedChanged="setText(this, document.getElementById('RadioButton1'))"
AutoPostBack="true"
/>
<asp:RadioButton
ID="RadioButton1"
runat="server"
Text="Red"
GroupName="group1"
/>
<script runat="server">
protected void setText(checkBox, radioBtn)
{
if (checkBox.Checked == true) {
radioBtn.Text = "abc";
}
else{
radioBtn.Text = "xyz";
}
}
</script>
function JsChangeRBText(itemCheckBox, rbClientId)
{
var optVal = document.getElementById(rbClientId);
var label = optVal.nextSibling;
if(itemCheckjBox.checked)
{
label.textContent = "adc";
}
else
{
label.textContent = "ccc";
}
}
Use textContent. This work for me.