radiobutton checked on condition in jquery

Posted by RememberME on Stack Overflow See other posts from Stack Overflow or by RememberME
Published on 2010-04-23T19:48:57Z Indexed on 2010/04/23 19:53 UTC
Read the original article Hit count: 277

Filed under:
|
|

I have the following fields:

            <label>Company Type:</label>
            <label for="primary"><input onclick="javascript: $('#sec').hide('slow');$('#primary_company').find('option:first').attr('selected','selected');" type="radio" runat="server" name="companyType" id="primary" />Primary</label>
            <label for="secondary"><input onclick="javascript: $('#sec').show('slow');" type="radio" runat="server" name="companyType" id="secondary" />Secondary</label>
            <div id="sec">
            <fieldset>
            <label for="primary_company">Primary Company:</label>
            <%= Html.DropDownList("primary_company", Model.SelectPrimaryCompanies, "** Select Primary Company **") %>
            </fieldset>

If there is a primary_company, then the secondary radio button should be selected. If there is no primary_company, then the primary radio button should be selected.

Here is my jQuery:

$(document).ready(function() {
        if ($('#primary_company').val().length > 0) {
            $('#secondary').attr({ checked: true });
        }
        else {
            $("#primary").attr('checked', true );
            $('#sec').hide();
        }

The sec div hides and shows properly, but a radio button is never selected. I've tried .attr('checked', 'checked') and .attr({ checked: true }) and .attr('checked', true) but nothing is ever selected.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about radiobutton