Checked for condition in jQuery

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 must be selected. If there is no primary_computer, you must select the primary radio button.

Here's my jQuery:

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

      

The sec section is hidden and displayed correctly, but the radio button is never selected. I tried .attr('checked', 'checked')

both .attr({ checked: true })

and .attr('checked', true)

and but nothing was selected.

EDIT I have just tried it .attr('checked', 'true')

Could there be something wrong with my IDs? I am stuck.

+2


a source to share


2 answers


Try to remove runat="server"

in input tags for radio buttons.



+1


a source


Syntax:



$ ('# secondary') attr ('checked', 'true') ;.

+3


a source







All Articles