1

I have a list box which is in Multiple selection mode but on a particular radio button click i want to change it as Single selection mode. Please help me out

  <tr>
                                    <td style="text-align: right; vertical-align: top; width: 21%;">
                                        <asp:Label ID="Label4" runat="server" Text="Do ypu want to apply NET/SET/PH.D. rule" ></asp:Label>
                                    </td>
                                    <td align="left" style="vertical-align: top;">
                                        :
                                    </td>
                                    <td align="left">
                                        <asp:RadioButtonList ID="RB_applyRule" runat="server" RepeatDirection="Horizontal" 
                                               onclick="radioButtonClick();"
                                            onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" >
                                                <asp:ListItem Text="Yes   &#160;&#160;&#160;&#160;&#160;&#160;" Value="0" Selected="True" ></asp:ListItem>
                                                <asp:ListItem Text="No" Value="1" ></asp:ListItem>
                                            </asp:RadioButtonList>
                                        &nbsp; <font class="Mandatory">*</font>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="text-align: right; vertical-align: top; width: 21%;">
                                        <asp:Label ID="lblDesignation" runat="server" Text="Seniority for designation" meta:resourcekey="lblDesignationResource1"></asp:Label>
                                    </td>
                                    <td align="left" style="vertical-align: top;">
                                        :
                                    </td>
                                    <td align="left">
                                        <asp:ListBox ID="List_Designation" runat="server" Width="306px" SelectionMode="Multiple"
                                            Rows="1"></asp:ListBox>
                                        &nbsp; <font class="Mandatory">*</font>
                                    </td>
                                </tr>
                                <tr>

// Javascript

    function radioButtonClick() {
        var rbList = document.getElementById('<%=RB_applyRule.ClientID%>');
        var rbCount = rbList.getElementsByTagName("input");
        var targRB;
        for (var i = 0; i < rbCount.length; i++) {
            if (rbCount[i].checked == true) {
                // var labelArray = rbCount[i].parentNode.getElementsByTagName('label');
                // targlist = targlist + labelArray[0].innerHTML + ", ";     // Get Label of Radiobutton
                targRB = rbCount[i].value;
             //   alert(targRB);
            }
        }
        if (targRB == "0") {
            alert("Yes");
        }
        if (targRB == "1") {
            alert("No");
        }
    }

// When alert says yes means i select yes than designation id List_Designation will be change in select mode.

1
  • create a jsfiddle and you'll be more likely to get an answer... Commented Apr 16, 2014 at 6:40

2 Answers 2

0

You can use jquery-ui-multiselect-widget plugin. It's exactly what you are looking for IMO. There you can set multiple: false if you want to change the mode to single selection mode.

$("select").multiselect({
   multiple: false,
   header: "Select an option",
   noneSelectedText: "Select an Option",
   selectedList: 1
});

Check the demos and code here. If you don't want to use the plugin, just check its code, you'll get the logic, hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I have given some hint of code. As my requirement is regarding List Box
@user1280492: Check this: stackoverflow.com/a/1071531/257635, modify it such that on click on a particular button you set item.Selected = false; for all except one.
Its server side code, as i want in javascript only'
0

HTML:

<input type="checkbox" id="toggle"/> 

<select id="my-select">
    <option value="foo">Foo</option>
    <option value="bar">Bar</option>
    <option value="baz">Baz</option>
    <option value="bat">Bat</option>
</select>

JavaScript:

(function () {
    var $checkbox = document.getElementById('toggle'),
        $select   = document.getElementById('my-select');

    $checkbox.addEventListener('click', function () {
        $select.multiple = !$select.multiple;   
    });
}());

Demo: http://jsfiddle.net/KV35g/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.