0

Hi I have a radio button list

<asp:RadioButtonList ID="lista_ragionevolezza" runat="server" 
 DataSourceID="EntityDataSource_ragionevolezza" 
 DataTextField="descrizione" DataValueField="id" 
 SelectedValue='<%# Bind("id_ragionevolezza") %>' 
 class="radioButtonScegli" />

and I want to select this in a Javascript function using JQuery for clearing a selection

var rbList=$("." + "radioButtonScegli");

using this class selector, but the code didn't work. What is the correct way to select this object? Thanks

Marco
  • 22,856
  • 9
  • 75
  • 124
P_R
  • 356
  • 2
  • 7
  • 27
  • What didn't work? Have you tried [debugging your Javascript](http://stackoverflow.com/q/988363/542251)? – Liam Oct 28 '13 at 11:15
  • Because if, for example, i try to find the length (rbl.rows.length) i have no results and an error "Cannot read property 'length' of undefined" – P_R Oct 28 '13 at 11:24
  • Why would that element have a rows property? if you did rbl.length that'd work, but rows is not a valid DOM element. I think your getting your Server side and client side mixed up. Inspect the actual HTML and see how it is actually rendered. – Liam Oct 28 '13 at 11:27
  • You also don't need the `+` just do `var rbList=$(".radioButtonScegli");` – Liam Oct 28 '13 at 11:28
  • ususally i use a rows property in radiobuttonlist client side. I try rbl.length but i doesn't work – P_R Oct 28 '13 at 11:30

4 Answers4

2

The class is added to a container in which .Net is placing those inputs. Something like this should work: $(".radioButtonScegli").find(':radio');

For clearing selection:

$('.radioButtonScegli').find(':radio').prop('checked',false);'

Rudy
  • 2,323
  • 1
  • 21
  • 23
0

By ID

var radiolist= $('input:radio[id$="lista_ragionevolezza"]');

By class

var radiolist= $('input:radio[class="radioButtonScegli"]');

or just $('.radioButtonScegli')

to make checked/unchecked you can use either attr or prop

$('.radioButtonScegli').find(':radio').attr('checked',false);

$('.radioButtonScegli').find(':radio').prop('checked',false);
0

I suggest to not use the class selector, but the Client Id, because your javascript variable would change if you have more the 1 object which has the class radioButtonScegli.

Try selecting it this way:

var rbList = $('#<%=lista_ragionevolezza.ClientID%>')

This way rbList will only contain this one RadiobuttonList. And since this is an element containing 1 to n checkboxes, you need to iterate over each item to uncheck it:

rbList.each(function (i, chkbox){
    if($(chkbox).is(":checked")) {
        $(chkbox).removeAttr("checked");
    }
});
Marco
  • 22,856
  • 9
  • 75
  • 124
-1

You have to use the ID of object:

var rblist = $("#lista_regionevolezza");
Liam
  • 27,717
  • 28
  • 128
  • 190
Dean.DePue
  • 1,013
  • 1
  • 21
  • 45