יום ראשון, 12 במאי 2013

using switch in javascript

The following script demonstrate the use of switch statement in javascript
<!DOCTYPE html>
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
   <body>
       <p>Click the button to display the current Date.</p
       <button id="GetCurrentDateBtn">Get current Date</button>
        <p id="CurrentDate"></p>
</body>

</html>
<script src="Scripts/jquery-1.7.1.js"></script>
<script>

    $(document).ready(function () {
        $("#GetCurrentDateBtn").on("click", function ()
        {
            HandleTheCurrentDateDispaying();
        });     
    });

    function HandleTheCurrentDateDispaying()
    {
        var theDayDiscreption ;
        var theDay = new Date().getDay();
        switch (theDay) {
            case 0:
                theDayDiscreption = "Sun";
                break;
            case 1:
                theDayDiscreption = "Mon";
                break;
            case 2:
                theDayDiscreption = "tue";
                break;         
            default:
                theDayDiscreption = "Sat";
        }

        $("#CurrentDate").text("<i>" + theDayDiscreption + "</i>");
    }
       
</script>

אין תגובות:

הוסף רשומת תגובה