Question

Using jQuery I want to be able to click an element which will also checks it's related radio button. I had this working fine until we had to add runat="server" to the radio buttons.

When I apply this it prevents my jQuery function from working and I cant figure out how to get round it, heres a simplified version of the code:

HTML

<input type="radio" runat="server" id="sector1Radio" name="SectorGroup" title="Sector1" />

jQuery

$('#SomethingElse').click(function() {
    $('input[title=Sector1]').attr('checked','checked');
});

I've found out that when its converted to a .net control instead of checked="checked" (as it would be usually) it is just Checked, so I changed that but on inspecting the DOM in multiple browsers, none of my radio buttons are being checked :-(

Are there any other ways I can use jQuery to check a radio button that has runat="server"?

Cheers!

Was it helpful?

Solution

I think that Your problem is that the id of the input is no longer sector1Radio but rather ctl00_sector1Radio or something similar. This happens if Your input control is inside e.g. a ContentPlaceHolder control (when using master pages).

Can You check the generated HTML code (in the browser) to verify if this is the case? What is the id of the input control?

If this is the case, You need to generate Your js jQuery code

$('#SomethingElse').click(function() {
  $('input[title=Sector1]').attr('checked','checked');
});

from codebehind so that SomeThingElse is replaced with the ClientID of the control.

OTHER TIPS

.is(':checked') works on ASP.NET radiobuttons and checkboxes

$('#SomethingElse').click(function() {
    $('input[title=Sector1]').is(':checked');
});

try using

$('input[title=Sector1]').attr('checked',true);

and

$('input[title=Sector1]').attr('checked',false);

or maybe

$('#SomethingElse').click(function () {
    $('input[title=Sector1]').attr('checked',!$('input[title=Sector1]').attr('checked'));
});

As suggested by others, ASP.net will not generate the html with the same ID you specified.

Quick solutions:

  • You can keep using the id but asks jquery to check the end of the id instead, example:

    $("input[id$='sector1Radio']").is(":checked");

  • Or check against the title and name as Nico suggested

  • Use the class element which is not effected by ASP.net, example

    <input type="radio" runat="server" id="sector1Radio" class="sector1Radio" name="SectorGroup" title="Sector1" />

    $("input.sector1Radio").is(":checked");

Best thing is to view the generated html code and see what id is giving you, then you can use the appropriate jquery selector, because the generated id could have different extensions depends whether you use master pages, etc.

If you are using a MasterPage or are creating the controls dynamically then it is probable that the control ID's are being renamed #SomethingElse becomes #MainContent_SomethingElse.

The easiest way to check this is to use the WebDeveloper plugin for Firefox or Chrome. Go to Information -> Display Element Information and then select the object in question. It will give you it's ID, class, as well as ancestor and children information. Check to see if the ID is being changed dynamically by the .NET.

If that's the case:

To prevent this, in the server side code you can use the following attribute to create static ID's

SomethingElse.ClientIDMode = ClientIDMode.Static;

You can then reference in you jQuery

$('#SomethingElse').click(function() {
            if ($('input[title=Sector1]').attr('checked')) {
             //execute event
});

I think what happens is that in ASP NET Checkboxes and Radio Buttons generates an "input" and a "span" after the input. So you need to select the input only.

You can try:

$('.classname input[type=checkbox]').each(function() {
        this.checked = true;
});

Two things here: finding the control and executing the check. In ASP.NET, your control's actual ID and name will end up getting changed based on the runat="server" containers in which it appears, even if those containers have no Ids.

Rendered ASP.NET controls always end with the same name as you started with, so a tag like:

<input type="radio" runat="server" id="sector1Radio" title="Sector1" />

might end up being rendered as

<input type="radio" runat="server" id="ctl0$ctl0$sector1Radio" name="ctl0_ctl0_SectorGroup" title="Sector1" />

You can find this element, even after it is rendered if you use the "contains" selection syntax in JQuery. So to find this element, once rendered, you could use:

$("input[type='radio'][id*='$sector1Radio']")

This syntax will find any radio button whose id contains "$sector1Radio"

Once you have the element, you can check or uncheck it using the following code, which you'd call from the click event of your other element.

// check the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', true);    

// uncheck the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', false);   

One last thing... if you just want a block of text to click the button when pressed (wrap it in an tag and set the AssociatedControlId property to the control name of your radio button, like this...

<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
<asp:label runat="server" id="lblsector1Radio" associatedControlID="sector1Radio">clicking here clicks and unclicks the radio button</asp:label>

I had the same problem. To use the jQuery UI to make your radiobuttons nice one has to write:

<div id="radio">
<input type="radio" id="radio1" runat="server" />
<label for="radio1">The label of the radio button</label>
...
</div>

<script type="text/javascript">
$('#radio').buttonset();
</script>

The id of the input tag must be correctly referenced by the label's for attribute. If the webpage is inside a master page then the id of the input tag will be modified to something like ctl00_Something_radio1, and suddenly the label's for attribute no longer references the input tag. Beware of this in ASP.NET!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top