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.
EDIT
I've now tried .attr('checked', 'true')
Could there be something wrong with my ids maybe? I'm stuck.