onlinecode

show and hide div elements based on dropdown selection in jQuery

show and hide div elements based on dropdown selection in jQuery

In this post we will give you information about show and hide div elements based on dropdown selection in jQuery. Hear we will give you detail about show and hide div elements based on dropdown selection in jQueryAnd how to use it also give you demo for it if it is necessary.

Use the jQuery change() method

The following example will demonstrate you how to show and hide div elements based on the dropdown selection or selected option in a select box using the jQuery change() method in combination with the show() and hide() methods. The div boxes in this example are hidden by default using the CSS displayproperty which value is set to none.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Select Box</title>
<style>
    .box{
        color: #fff;
        padding: 20px;
        display: none;
        margin-top: 20px;
    }
    .red{ background: #ff0000; }
    .green{ background: #228B22; }
    .blue{ background: #0000ff; }
</style>
<script src="https://.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $("select").change(function(){
        $(this).find("option:selected").each(function(){
            var optionValue = $(this).attr("value");
            if(optionValue){
                $(".box").not("." + optionValue).hide();
                $("." + optionValue).show();
            } else{
                $(".box").hide();
            }
        });
    }).change();
});
</script>
</head>
<body>
    <div>
        <select>
            <option>Choose Color</option>
            <option value="red">Red</option>
            <option value="green">Green</option>
            <option value="blue">Blue</option>
        </select>
    </div>
    <div >You have selected <strong>red option</strong> so i am here</div>
    <div >You have selected <strong>green option</strong> so i am here</div>
    <div >You have selected <strong>blue option</strong> so i am here</div>
</body>
</html

Hope this and post will helped you for implement show and hide div elements based on dropdown selection in jQuery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version