Saturday, 11 June 2016

How to delete selected item from dropdown list using JavaScript/jQuery

Description:
 In this example we explain that how to delete selected item from dropdown list using JavaScript/jQuery in asp.net.or how to remove selected item from dropdown list using JavaScript or jQuery in asp.net.

Sometime we have requirement like remove dropdown list items/option when something occur like for example if we select category is equals to vegetable then  only vegetable are shown in other dropdown and other items of dropdown list will be removed.

So below is the example that demonstrates to remove/delete item in dropdown list using client side script like jQuery or JavaScript.
Code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Delete (Remove) selected Item (Option) from DropDownList using JavaScript and jQuery</title>
</head>
<body>
    <form id="form1" runat="server">
    <fieldset>
    <legend>Javascript</legend>

   <select id="ddlCountry" style="width: 150px;">
    <option value="1">India</option>
    <option value="2">Pakistan</option>
    <option value="3">USA</option>
    <option value="4">UK</option>
</select>

<br />
<hr />
<input type="button" id="btnDelete" value="Delete" onclick="DeleteValues();"/>
</fieldset>
<script type="text/javascript">
    function DeleteValues() {
        var dropDown = document.getElementById("ddlCountry");
        for (var i = 0; i <= dropDown.options.length; i++) {
            if (dropDown.options[i].selected) {
                alert(dropDown.options[i].text + " is removed from dropdownlist");
                dropDown.removeChild(dropDown.options[i]);
                break;

            }
        }
    }
</script>

    </form>
</body>
</html>
 

0 comments:

Post a Comment