Omi
Omi

Reputation: 447

Not able to show/hide div using Jquery on Asp.Net RadioButtonList item selection?

I have a asp.net RadioButtonList Control and a I want to hide or display these based on RadioButtonList selection change using Jquery. Please guide me why I am not able to do it when using the same code with in

My aspx

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"
type = "text/javascript"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
type = "text/javascript"></script> 
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
rel = "Stylesheet" type="text/css" />
<script type="text/javascript">
    $(document).ready(function () {
        $('#rdbrawmtrl input').click(function () {
            var value = $('#rdbrawmtrl input:checked').val();
            if (value == "Cotton") {
                $("#abc").show();
                $("#Panel6").hide();
            }
            if (value == "Wool") {
                $("#Panel6").show();
                $("#abc").hide();
            }
        });
    });
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:RadioButtonList ID="rdbrawmtrl" runat="server" Style="margin: 0 4px 8px 0;">
                            <asp:ListItem>Cotton</asp:ListItem>
                            <asp:ListItem>Wool</asp:ListItem>
                </asp:RadioButtonList>
<div id="abc" runat="server" style="display:none;">
//Some more controls
</div>
<div id="Panel6" runat="server" style="display:none;">
//some more controls
</div>
</asp:Content>

I have even tried to display jquery variable "value" but it showing no result.

<script type="text/javascript">
    $(document).ready(function () {
        $('#rdbrawmtrl input').click(function () {
            var value = $('#rdbrawmtrl input:checked').val();
            alert(value);
        });
    });
</script>

Please guide me where I am doing wrong.

Upvotes: 0

Views: 1046

Answers (3)

Zohaib Waqar
Zohaib Waqar

Reputation: 1239

you can not get HTML Element through id in javascript directly. ASP.net is server side language and Javascript is client side so you need to get CLient ID . Try this ..

$('#<%=rdbrawmtrl.ClientID%>').click(function(){
      // your code here ....
});

Upvotes: 1

Rahul Singh
Rahul Singh

Reputation: 21795

You are not able to retrieve value because your ASP.NET RadioButton is rendered like this in browser:-

<tr>
  <td>
     <input name="rdbrawmtrl" id="rdbrawmtrl_0" type="radio" value="Cotton">
     <label for="rdbrawmtrl_0">Cotton</label>
  </td>
</tr>

<tr>
  <td>
     <input name="rdbrawmtrl" id="rdbrawmtrl_1" type="radio" value="Wool">
     <label for="rdbrawmtrl_1">Wool</label>
  </td>
</tr>

As you can see the Id will be generated dynamically for each radio button. You should write a selector on name attribute like this:-

$('input[name="rdbrawmtrl"]').click(function () {
     var value = $(this).val();
     alert(value);
});

Update:

Having a Master Page will not only change the Id dynamically but also the name attribute. You can add a class to your RadioButtonList like this:-

<asp:RadioButtonList ID="rdbrawmtrl" runat="server" Style="margin: 0 4px 8px 0;" 
                      CssClass="clsrdbrawmtrl">

Then you can find the radio buttons present inside it like this:-

$('input[type="radio"]',$('.clsrdbrawmtrl')).click(function () {
     alert($(this).val());
});

Upvotes: 0

lshettyl
lshettyl

Reputation: 8181

I have assumed that ASP renders your radio buttons as below.

$("input[name='rdbrawmtrl']:radio").change(function() {

    var val = $(this).val();
    alert ( val );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td>
      <input name="rdbrawmtrl" id="rdbrawmtrl_0" type="radio" value="Cotton" />
     <label for="rdbrawmtrl_0">Cotton</label>
  </td>
</tr>

<tr>
  <td>
      <input name="rdbrawmtrl" id="rdbrawmtrl_1" type="radio" value="Wool" />
     <label for="rdbrawmtrl_1">Wool</label>
  </td>
</tr>

Edit: Based on your comment, try the following.

$("input[id^='ContentPlaceHolder1_rdbrawmtrl']:radio").change(function() {

    var val = $(this).val();
    alert ( val );
});

Upvotes: 1

Related Questions