views:

363

answers:

3

I have a set of radio buttons. When primary is selected, the Primary Company field is hidden. I would also like to at this time clear the drop down selection. How can I do this?

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

    </p>
+2  A: 

You can clear the selection (that is, select the first option) like so:

$("#primary_company").find('option:first')
                     .attr('selected','selected');
karim79
+1. but why the `.parent('select');` part? You could just use `.end()`, right?
David Murdoch
@David - that was part of a longer chain to serve an evil purpose. (I pasted too much code from one of my test pages), but now that you point that out, `end` is better than `parent('select')`. Thanks.
karim79
A: 

To clear the selection completely (so that not even the first element is selected) you could use:

$('#primary_company').attr('selectedIndex', '-1'); 

If #primary_company is a multiple select box you could use:

$('#primary_company option').attr('selected', false);
prendio2
A: 
$("#primary_company").find('[selected]').removeAttr("selected");
David Murdoch