Carlos Siles
Carlos Siles

Reputation: 71

get ASPX radiobutton text or value from javascript

ASPX radiobuttons are inside a datalist template

<asp:RadioButton ID="RadioButton1" runat="server"  
        GroupName="rdb" 
        Text='<%#  Eval("type1") %>'  
        onclick="Getr1(this)"/>

JavaScript

function getr1(val){
    alert(val)
}

if I use this function in any html control, it will return a value but it is not working in the aspx radiobuttons. I used this code: onchange="GetQty(this.options[this.selectedIndex].value)" to get the selected index from an aspx dropdownlist and it works just fine, maybe someone can help me to figure out the correct syntax for aspx rb. I already tried onclick="Getr1(this.options[this.checked].value)", THANKS IN ADVANCE

Upvotes: 1

Views: 3092

Answers (2)

Avishek
Avishek

Reputation: 1896

Your code is fine, just need a little change.

<asp:RadioButton ID="RadioButton1" runat="server"  
    CliientIDMode="Static" GroupName="rdb" 
    Text='<%#  Eval("type1") %>' />

and

 <asp:Label ID="Label1" runat="server" style="font-size: x-small"
    Text='<%# Eval("type1") %>'></asp:Label>

jQuery for getting RadioButton Text:

$("#RadioButton1").click(function{
    alert( $(this).siblings('label').text());
});

jQuery for getting asp:Label Text next to the RadioButton:

$("#RadioButton1").click(function{
    alert( $(this).siblings('span').text());
});

Hope it helps :)

Upvotes: 1

codeandcloud
codeandcloud

Reputation: 55200

Try this JavaScript.

function Getr1(elm) {
    var nextElm = elm.nextSibling;
    //catching white space, comments etc...
    while (nextElm && nextElm.nodeType != 1) {
        nextElm = nextElm.nextSibling
    }
    alert(nextElm.innerHTML);
}

Reason: asp:RadioButton is rendered like this in the browser

<input id="MainContent_RadioButton1" type="radio" onclick="Getr1(this);" 
     value="RadioButton1" name="ctl00$MainContent$rdb">
<label for="MainContent_RadioButton1">your rendered text here</label>

And if you use jQuery, it will be as simple as this.

function Getr1(elm) {
    alert( $(elm).next().html() );
}

Upvotes: 0

Related Questions