Reputation: 297
so Im using this code for show/hiding 3 divs. and it works like a charm.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Using Checkboxes</title>
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
function getCookieValue(a) {
var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
$(document).ready(function(){
var redEl = $('input[value="red"]');
var greenEl = $('input[value="green"]');
var blueEl = $('input[value="blue"]');
if (document.cookie.indexOf('red=') != -1) {
redEl.prop("checked", $.parseJSON(getCookieValue("red")));
}
if (document.cookie.indexOf('green=') != -1) {
greenEl.prop("checked", $.parseJSON(getCookieValue("green")));
}
if (document.cookie.indexOf('blue=') != -1) {
blueEl.prop("checked", $.parseJSON(getCookieValue("blue")));
}
$(".red").toggle(redEl.prop("checked"));
$(".green").toggle(greenEl.prop("checked"));
$(".blue").toggle(blueEl.prop("checked"));
$('input[type="checkbox"]').click(function(){
var expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 1);
expiryDate = expiryDate.toUTCString();
if($(this).attr("value")=="red"){
$(".red").toggle(this.checked);
document.cookie="red=" + this.checked.toString() + "; expires=" + expiryDate;
}
if($(this).attr("value")=="green"){
$(".green").toggle(this.checked);
document.cookie="green=" + this.checked.toString() + "; expires=" + expiryDate;
}
if($(this).attr("value")=="blue"){
$(".blue").toggle(this.checked);
document.cookie="blue=" + this.checked.toString() + "; expires=" + expiryDate;
}
});
});
</script>
</head>
<body>
<div>
<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>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
</body>
</html>
But i want to add little bit more to it. here is what i want:
Div red is 50% width Green is 50% width Blue is 100% width.
what i want is something like: If green is showing/checked, then div red is 50%. if div green is unchecked/turned of, then div red changes to 100%. the blue one is normal all the time and nothing changed with that.
Upvotes: 0
Views: 230
Reputation: 6992
just add a small if condition in your click event work
if($(this).val() == 'green'){
width = ($(this).prop('checked'))?'50%':'100%';
$(".red").css("width",width)
}
Js Fiddle to make this work on page load you need to add some code where you reading cookies and making check box checked as follow.
if (document.cookie.indexOf('green=') != -1) {
greenEl.prop("checked", $.parseJSON(getCookieValue("green")));
if(getCookieValue("green")){ // if green checkbox is check
$(".red").css("width","50%") // make it,s width 50%
}
}
Upvotes: 1
Reputation: 1306
Please try below mentioned solution.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Using Checkboxes</title>
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
function getCookieValue(a) {
var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
$(document).ready(function(){
var redEl = $('input[value="red"]');
var greenEl = $('input[value="green"]');
var blueEl = $('input[value="blue"]');
if (document.cookie.indexOf('red=') != -1) {
redEl.prop("checked", $.parseJSON(getCookieValue("red")));
}
if (document.cookie.indexOf('green=') != -1) {
greenEl.prop("checked", $.parseJSON(getCookieValue("green")));
}
if (document.cookie.indexOf('blue=') != -1) {
blueEl.prop("checked", $.parseJSON(getCookieValue("blue")));
}
$(".red").toggle(redEl.prop("checked"));
$(".green").toggle(greenEl.prop("checked"));
$(".blue").toggle(blueEl.prop("checked"));
$('input[type="checkbox"]').click(function(){
var expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 1);
expiryDate = expiryDate.toUTCString();
if($(this).attr("value")=="red"){
$(".red").toggle(this.checked);
document.cookie="red=" + this.checked.toString() + "; expires=" + expiryDate;
}
if($(this).attr("value")=="green"){
$(".green").toggle(this.checked);
document.cookie="green=" + this.checked.toString() + "; expires=" + expiryDate;
if(this.checked)
$(".red").css("width","50%");
else
$(".red").css("width","100%");
}
if($(this).attr("value")=="blue"){
$(".blue").toggle(this.checked);
document.cookie="blue=" + this.checked.toString() + "; expires=" + expiryDate;
}
});
});
</script>
</head>
<body>
<div>
<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>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
</body>
</html>
Upvotes: 1
Reputation: 453
Made a fiddle for you. Removed some of the functionality in your example, but hopefully the essence is there. Also, I don't think it answers your question perfectly, but it's a place to start if you want to make changes based on which box is checked. The essence is quite simple, just use a event listener to do modifications to the css of the individual divs you want to edit:
redEl.on("change",function(){
<your logic here>
});
Hope this helps
Upvotes: 0
Reputation: 123
Try It: Its 100% workable...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Using Checkboxes</title>
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
function getCookieValue(a) {
var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
$(document).ready(function(){
var redEl = $('input[value="red"]');
var greenEl = $('input[value="green"]');
var blueEl = $('input[value="blue"]');
if (document.cookie.indexOf('red=') != -1) {
redEl.prop("checked", $.parseJSON(getCookieValue("red")));
}
if (document.cookie.indexOf('green=') != -1) {
greenEl.prop("checked", $.parseJSON(getCookieValue("green")));
}
if ($('#chkgreen').is(':Checked')) {
$(".red").css('width', '50%');
}
else {
$(".red").css('width', '96.8%');
}
if (document.cookie.indexOf('blue=') != -1) {
blueEl.prop("checked", $.parseJSON(getCookieValue("blue")));
}
$(".red").toggle(redEl.prop("checked"));
$(".green").toggle(greenEl.prop("checked"));
$(".blue").toggle(blueEl.prop("checked"));
$('input[type="checkbox"]').click(function(){
var expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 1);
expiryDate = expiryDate.toUTCString();
if($(this).attr("value")=="red"){
$(".red").toggle(this.checked);
document.cookie="red=" + this.checked.toString() + "; expires=" + expiryDate;
}
if($(this).attr("value")=="green"){
$(".green").toggle(this.checked);
document.cookie="green=" + this.checked.toString() + "; expires=" + expiryDate;
}
if($(this).attr("value")=="blue"){
$(".blue").toggle(this.checked);
document.cookie="blue=" + this.checked.toString() + "; expires=" + expiryDate;
}
});
});
</script>
</head>
<body>
<div>
<label>
<input type="checkbox" name="colorCheckbox" value="red" id="chkred" />
red</label>
<label>
<input type="checkbox" name="colorCheckbox" value="green" id="chkgreen"/>
green</label>
<label>
<input type="checkbox" name="colorCheckbox" value="blue" id="chkblue"/>
blue</label>
</div>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
</body>
</html>
Upvotes: 2