This is another important concept to select the multiple value from one select box with the help of check box using jquery. Commonly we will select one value from the <select> box , It shows only option values from the select box like a dropdown list. Some times we need to get multiple value from the select box so that kind of places we need to use Jquery and css concept. Here is the solution to get more then one value from the select box
.
.
Here there are few jquery function used to set custom selectbox like toggle(), hide(), show(). To Set option box for each option in the select box. Each options captured its value ans stored using php function implode(); This example contains full code and demo pages for your reference. This is very useful to select multiple value from the select box. Please check the demo page for live demo of this project.
Code for multiple select from selectbox:
<html>
<head>
<title>Select Multiple value from select box with check box</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="multiple-select-container">
<form id="fromCustomSelect" name="fromCustomSelect" action=""
method="post">
<div>
<?php
if(!empty($_POST["subjects"])) {
$selectedValues = implode(", ", $_POST["subjects"]);
?>
<div class="result-list">
<div class="result-list-heading">The Selected subjects: </div>
<div><?php echo $selectedValues; ?></div>
</div>
<?php
}
?>
<div class="multiple-select" id="multiple-select">Select Your Subjects</div>
<div id="multiple-select-option-box">
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="Tamil"> Tamil
</div>
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="English"> English
</div>
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="Maths"> Maths
</div>
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="Science"> Science
</div>
</div>
</div>
<button type="submit" class="search btn">Submit</button>
</form>
<br/><br/>
<h2 style="text-align:right; color:#fff;"><a style="text-align:right; color:#fff;" href="http://www.sanwebcorner.com">www.sanwebcorner.com</a></h2>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$("#multiple-select").on("click",function(){
$("#multiple-select-option-box").toggle();
});
function toggleFillColor(obj) {
$("#multiple-select-option-box").show();
if($(obj).prop('checked') == true) {
$(obj).parent().css("background",'#c6e7ed');
} else {
$(obj).parent().css("background",'#FFF');
}
}
$(".multiple-select-option").on("click", function() {
var checkboxObj = $(this).children("input");
$(checkboxObj).prop("checked",true);
toggleFillColor(checkboxObj);
});
$("body").on("click",function(e){
if(e.target.id != "multiple-select" && $(e.target).attr("class") != "multiple-select-option") {
$("#multiple-select-option-box").hide();
}
});
</script>
</body>
</html>
<head>
<title>Select Multiple value from select box with check box</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="multiple-select-container">
<form id="fromCustomSelect" name="fromCustomSelect" action=""
method="post">
<div>
<?php
if(!empty($_POST["subjects"])) {
$selectedValues = implode(", ", $_POST["subjects"]);
?>
<div class="result-list">
<div class="result-list-heading">The Selected subjects: </div>
<div><?php echo $selectedValues; ?></div>
</div>
<?php
}
?>
<div class="multiple-select" id="multiple-select">Select Your Subjects</div>
<div id="multiple-select-option-box">
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="Tamil"> Tamil
</div>
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="English"> English
</div>
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="Maths"> Maths
</div>
<div class="multiple-select-option">
<input onchange="toggleFillColor(this);"
class="multiple-select-option-checkbox" type="checkbox"
name="subjects[]" value="Science"> Science
</div>
</div>
</div>
<button type="submit" class="search btn">Submit</button>
</form>
<br/><br/>
<h2 style="text-align:right; color:#fff;"><a style="text-align:right; color:#fff;" href="http://www.sanwebcorner.com">www.sanwebcorner.com</a></h2>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$("#multiple-select").on("click",function(){
$("#multiple-select-option-box").toggle();
});
function toggleFillColor(obj) {
$("#multiple-select-option-box").show();
if($(obj).prop('checked') == true) {
$(obj).parent().css("background",'#c6e7ed');
} else {
$(obj).parent().css("background",'#FFF');
}
}
$(".multiple-select-option").on("click", function() {
var checkboxObj = $(this).children("input");
$(checkboxObj).prop("checked",true);
toggleFillColor(checkboxObj);
});
$("body").on("click",function(e){
if(e.target.id != "multiple-select" && $(e.target).attr("class") != "multiple-select-option") {
$("#multiple-select-option-box").hide();
}
});
</script>
</body>
</html>
0 Comments