Enum (Enumeration) with jQuery

enum
Introduction:
An enumeration is a complete, ordered listing of all the items in a collection. The term is commonly used in mathematics and theoretical computer science (as well as applied computer science) to refer to a listing of all of the elements of a set.
This article explains how to use Enum (Enumeration) with jQuery. 

The following HTML Markup consists of an HTML Select DropDownList. The HTML Select DropDownList has been assigned a jQuery change event handler, when the DropDownList is changed the selected value is compared with the Enum and the respective color is displayed using JavaScript alert message box.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
 libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        var colors = { "Red": 1, "Green": 2, "Blue": 3 };
        $(function () {
            $("#ddlColors").change(function () {
                switch (parseInt($(this).val())) {
                    case colors.Red:
                        alert("Red");
                        break;
                    case colors.Green:
                        alert("Green");
                        break;
                    case colors.Blue:
                        alert("Blue");
                        break;
                }
            });
        });
    </script>
</head>
<body>
    <select id ="ddlColors">
        <option value="1">Red Color</option>
        <option value="2">Green Color</option>
        <option value="3">Blue Color</option>
    </select>
</body>
</html>
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment