Wednesday, 20 July 2016

How to show and hide div elements based on dropdown selection in jQuery

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>jQuery Show Hide Using Select Box</title>
    <style type="text/css">
        .hd1{
            padding: 20px;
            display: none;
            margin-top: 20px;
            border: 1px solid #000;
            background: #ff0000;
        }
   </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("select").change(function(){
            $(this).find("option:selected").each(function(){
                if($(this).attr("value")=="1"){
                    $(".hd1").show();
                }
                else{
                    $(".hd1").hide();
                }
            });
        }).change();
    });
    </script>
    </head>
    <body>
        <div>
            <select>
                <option>Choose Color</option>
                <option value="1">Red</option>
                <option value="2">Green</option>
                <option value="3">Blue</option>
            </select>
        </div>
        <div class="hd1">You have selected <strong>red option</strong> so i am here</div>
    </body>
 </html>

No comments:

Post a Comment