Loading ...

Want to remove some of the options of dropdownlist in client side

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  client side web development   » Want to remove some of the options of dropdownlist in client side

Want to remove some of the options of dropdownlist in client side

Posts under the topic: Want to remove some of the options of dropdownlist in client side

Posted: 6/27/2010

Lurker 235  points  Lurker
  • Joined on: 10/16/2009
  • Posts: 47

Hi experts,

 

I want to remove some of the options based on my condition from the dropdownlist, but I want to do it so in client side. Please help me. 


tags javascript

Posted: 6/27/2010

Guru 16518  points  Guru
  • Joined on: 4/19/2009
  • Posts: 483
  Answered

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

</head>
<body>
    <form runat="server">
        <select id="ddlFoo">
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
            <option value="4">Four</option>
            <option value="5">Five</option>
        </select>
        <br />
        <input id="btnFoo" type="button" value="Remove" />
    </form>
</body>
 <script type="text/javascript">
     $('#btnFoo').click(function () {
        //your logic here
         // for this case I will remove the option having values 3 and 5
         $("#ddlFoo option[value='3'],option[value='5']").remove();
     });
</script>
</html>


Posted: 6/29/2010

Professional 8495  points  Professional
  • Joined on: 5/3/2010
  • Posts: 389
  Answered

The same sample that @Raghav has shown using JQuery, I have rewritten using plain Javascript.

Here is the code:

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="Form1" runat="server">
<select id="ddlFoo">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>


<input id="btnFoo" type="button" onclick="removeElements(document.getElementById('ddlFoo'))" value="Remove" />
</form>
</body>
<script type="text/javascript">
function removeElements(elements) {
alert(elements.selectedIndex);
elements.options[elements.selectedIndex] = null;
}
</script>
</html>

 

Shortly, in this example I am removing the selected value from the DropDownList. After clicking the Remove button, the item will get removed.


All the best.


Page 1 of 1 (3 items)