How to show/hide div element depending multiple values using Bootstrap and jQuery ?
Last Updated :
03 Aug, 2021
Bootstrap and jQuery have been used together to develop interactive web applications. Both Bootstrap and jQuery are free and open source which makes it popular among developers. This article deals with showing and hiding division depending on user input with the help of Bootstrap and jQuery. Below shown are two approaches to establish the same.
First Approach: In the first method, only one division is visible at a time depending on the input from the user. This method comprises a drop down list with three options. The user can select one option at a time and depending on the value of the option the corresponding division is displayed. The method used in the code snippet are as follows:
- $(document).ready(function): The ready event is triggered when the document is loaded and the function passed as parameter is executed.
- $(selector).change(function): The change event is triggered when the selected element (input, textarea or select) has been changed and the function passed as a parameter is executed. Here the function passed as parameter is invoked when the user selects an option from the drop down menu.
- $(selector).find(filter): This method returns all the descendant elements of the selected element. Here <option> is the descendant of <select>. Also, the method requires a filter to return the required element. Here the selected option is the filter ie the option selected by the user is returned.
- $(selector).each(function): This method executed the function passed as parameter for each matched element. Here the function passed as parameter executes for each element returned by the find() method.
- $(selector).attr(attribute): This method set or return attributes and values of the selected elements respectively. Here the value of the selected option is extracted into the optionValue variable.
- $(selector).not(criteria): This method returns elements that do not match the specified criteria. Here the not method returns the options that are not selected by the users.
- $(selector).hide(): This method hides the selected element.
- $(selector).show(): This method shows the selected element.
Explanation: The page displays a drop down menu from which the user selects an option. As soon as a change occurs in the <select> element, the change() method is triggered which in turn invokes the find function and it returns the selected option. The attr() method extracts the value of the option into the optionValue variable. The optionValue variable is appended with '.' (which turns it into one among the defined division classes) in the not() method. The not() method hides all the classes that do not match the class mentioned in the criteria. The show() method finally displays the division as selected by the user. If no option is selected by the user then all the divisions are hidden.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Show Hide Elements Using Dropdown</title>
<!--CSS stylesheet-->
<style>
.box {
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.red {
background: red;
}
.green {
background: green;
}
.blue {
background: blue;
}
</style>
<!--importing jquery cdn-->
<script src=
"https://code.jquery.com/jquery-1.12.4.min.js">
</script>
<script>
// jQuery functions to hide and show the div
$(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>
<!--dropdown list options-->
<select>
<option>Choose Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
<!--divs that hide and show-->
<div class="red box">Red Selected</div>
<div class="green box">Green Selected</div>
<div class="blue box">Blue Selected</div>
</body>
</html>
Output:
Second Approach: This approach demonstrates how one or more divisions can be displayed simultaneously. This method makes use of checkboxes for taking user input. As check boxes are capable of taking more than one value at a time hence it is an ideal choice to serve our purpose. The following methods are used in the code:
- $(document).ready(function): The ready event is triggered when the document is loaded and the function passed as parameter is executed.
- $(selector).click(function): This method triggers the click event or executes a function when a click event occurs. Here as the user checks the checkbox the click event is triggered and the function passed as parameter is executed.
- $(selector).attr(attribute): This method returns the values of the selected elements. Here the value of the selected option is extracted into the inputValue variable.
- $(selector).toggle(): This method toggles between hide() method and show() method for the selected elements. If the box is checked the division is shown. If the box is unchecked the division is hidden.
Explanation: The webpage comprises certain checkboxes. The user may select one or more than one check boxes at a time. The divisions corresponding to the checkboxes are shown if they were hidden initially. If the checkboxes are unchecked then the corresponding division is hidden.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Show Hide Elements Using Checkboxes</title>
<!--CSS stylesheet-->
<style>
.box {
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.red {
background: red;
}
.green {
background: green;
}
.blue {
background: blue;
}
label {
margin-right: 15px;
}
</style>
<!--import jQuery cdn-->
<script src=
"https://code.jquery.com/jquery-1.12.4.min.js">
</script>
<script>
// jQuery functions to show and hide divisions
$(document).ready(function () {
$('input[type="checkbox"]').click(function () {
var inputValue = $(this).attr("value");
$("." + inputValue).toggle();
});
});
</script>
</head>
<body>
<div>
<!--checkboxes-->
<label><input type="checkbox"
name="colorCheckbox" value="red">
red
</label>
<label><input type="checkbox"
name="colorCheckbox" value="green">
green
</label>
<label><input type="checkbox"
name="colorCheckbox" value="blue">
blue
</label>
</div>
<!--Divisions to be shown and hidden-->
<div class="red box">Red Selected</div>
<div class="green box">Green Selected</div>
<div class="blue box">Blue Selected</div>
</body>
</html>
Output
jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more".
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
Similar Reads
How to Show and Hide div elements using radio buttons? In this article, we will see how we can show and hide div elements by clicking on the radio buttons. There are different methods available in jQuery to accomplish this task as listed below: Table of Content Using the hide() and show() methodsUsing the css() methodUsing the hide() and show() methodsT
3 min read
How to hide div element by default and show it on click using JavaScript and Bootstrap ? We have to first hide the div and when a user will click the button we need to display that hiden div on the screen. It is helpul for creating any type of form. There are two methods to solve this problem which are discussed below:Â Table of Content Using .show() methodUsing .toggle() methodApproach
2 min read
How to show/hide an element using jQuery ? We are given an element and the task is to to show/hide the element using jQuery. Below are the approaches to show/hide an element using jQuery:Table of ContentUsing css() methodsUsing show methodUsing Toggle methodApproach 1: Using css() methodsIt takes two parameters where the first parameter is t
3 min read
How to Show and Hide div elements using Checkboxes ? In order to display data/content of a specific element by selecting the particular checkbox in jQuery we can use the toggle() method. The toggle() method is used to check the visibility of selected elements to toggle between hide() and show() for the selected elements. Syntax: $(selector).toggle(spe
4 min read
How to Hide an HTML Element in Mobile View using jQuery ? Suppose we have given an HTML document and the task is to hide an HTML element in mobile view with the help of jQuery. Approach: Initially, we are required to detect whether our system is 'Mobile' or not, for that window.navigator.userAgent property is used. It returns a string containing informatio
2 min read
How to hide elements defined as variables in jQuery ? In this article, we will learn how to hide elements defined as variables in jQuery. These can be done using two approaches. Approach 1: In this approach, we will first select the element that has to be hidden and then assign it to a variable. We will then call the hide() method on the variable. This
2 min read
How to hide/show an image on button click using jQuery ? In this article, we will see how we can hide or show any particular image in jQuery when a button gets clicked. This is quite easy to do with some lines of jQuery code. Before we jump to the topic, let's know which methods of jQuery will be used for this. So there is a method called show() and anoth
3 min read
How to Toggle between Hiding and Showing an Element using JavaScript ? Toggle between hiding and showing an element using JavaScript provides the feature of efficient space usage by allowing users to hide content sections they may not need to minimize distractions, leading to a cleaner and more organized layout. Syntaxif (paragraph.style.display === 'none') { paragraph
3 min read
How to Show/Hide a Div Based on Selected Option Value of a Select Using JavaScript ? We are going to discuss how can we show/hide a div based on a selected option value. Select is one of the input type tags. It is used in HTML to get input from one of the options given below. Not only are we able to get select input (optional input), but we can also make dynamic changes to the websi
3 min read
How to display div elements using Dropdown Menu in jQuery? In order to display data/content of a specific element by selecting the particular dropdown menu in jQuery, we can use the with following ways: Using hide() and show() methods: hide() methods : This method is used to hiding the syntax or the element of html that you want to hide. Syntax: $(selector)
4 min read