JordanBlackler
asked on
change images when unchecking a checkbox using jquery
Hello -
Some details first:
I have 4 image place holders. I have several checkboxes. Each checkbox is associated with an image. User is allowed to check off 4 boxes.
When a user clicks on a checkbox it replaces one of the image place holders with the image that was associated with the checkbox.
After the user checks off 4 of the check boxes, the rest of the checkboxes become disabled.
So now we have the 4 image place holders populated with images from the checked check boxes.
Here is where my issue comes into play.
If the user decides to uncheck a box, i need the correct place holder image to go back to it's generic place holder image.
So if the 4 checkboxes the user checked were 1: Hammer 2: Screwdriver 3: Nails 4: Saw. Those 4 place holder images would be populated with a Hammer, Screwdriver, Nails and a Saw. If they decide to uncheck the Screwdriver checkbox then that placeholder image with the screwdriver should switch to the generic place holder image.
I'm having a hard time figuring out how to do that.
thanks in advance!
Below is the function that I need help with. My main issue is that when I uncheck a box it removes the wrong image from the place holder. It should get the value of the box I just unchecked and then check that value against the 4 places holders and see which one matches it, then change that place holder back to the generic place holder imager.
Some details first:
I have 4 image place holders. I have several checkboxes. Each checkbox is associated with an image. User is allowed to check off 4 boxes.
When a user clicks on a checkbox it replaces one of the image place holders with the image that was associated with the checkbox.
After the user checks off 4 of the check boxes, the rest of the checkboxes become disabled.
So now we have the 4 image place holders populated with images from the checked check boxes.
Here is where my issue comes into play.
If the user decides to uncheck a box, i need the correct place holder image to go back to it's generic place holder image.
So if the 4 checkboxes the user checked were 1: Hammer 2: Screwdriver 3: Nails 4: Saw. Those 4 place holder images would be populated with a Hammer, Screwdriver, Nails and a Saw. If they decide to uncheck the Screwdriver checkbox then that placeholder image with the screwdriver should switch to the generic place holder image.
I'm having a hard time figuring out how to do that.
thanks in advance!
Below is the function that I need help with. My main issue is that when I uncheck a box it removes the wrong image from the place holder. It should get the value of the box I just unchecked and then check that value against the 4 places holders and see which one matches it, then change that place holder back to the generic place holder imager.
function countChecked() {
var radioCandyCount = document.getElementsByName('HowManyCandies');
for (var i = 0, length = radioCandyCount.length; i < length; i++) {
if (radioCandyCount[i].checked) {
var a = radioCandyCount[i].value;
var b = "2";
var c = Number(a) + Number(b);
var n = $("input:checked").length;
document.getElementById("CandyCount").value = Number(c) - Number(n)
/* ********** THIS IS THE SECTION I'M HAVING A PROBLEM WITH. ***************/
if (document.getElementById("CandyCount").value == 4) {
$('#Jar1').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
$('#Jar2').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
$('#Jar3').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
$('#Jar4').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
if (document.getElementById("CandyCount").value == 3) {
if ($(this).not(':checked')) {
var unChecked = this.value;
//alert($('#Jar1').css('backgroundImage') + "_" + "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
if ($('#Jar1').css("backgroundImage") == "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
{
$('#Jar1').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
if ($('#Jar2').css("backgroundImage") == "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
{
$('#Jar2').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
if ($('#Jar3').css("backgroundImage") == "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
{
$('#Jar3').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
}
}
if (document.getElementById("CandyCount").value == 2) {
if ($(this).not(':checked')) {
var unChecked = this.value;
if ($('#Jar1').css("backgroundImage") == "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
{
$('#Jar1').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
if ($('#Jar2').css("backgroundImage") == "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
{
$('#Jar2').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
}
}
if (document.getElementById("CandyCount").value == 1) {
if ($(this).not(':checked')) {
var unChecked = this.value;
if ($('#Jar1').css("backgroundImage") == "url(\"http://www.AWebSite.com/Test/Website/Images/" + unChecked + ".png\"" + ")");
{
$('#Jar1').css("background-image", "url(http://www.AWebSite.com/Test/Website/Images/SingleJar.png)");
}
}
}
/* *****END***** THIS IS THE SECTION I'M HAVING A PROBLEM WITH. *******END********/
/* ********** THIS IS THE SECTION THAT CHANGES THE PLACE HOLDER IMAGE TO THE CHECKBOX IMAGE. ***************/
if (length == 3) {
if (document.getElementById("CandyCount").value == 3) {
$(':checkbox(:checked)')
if (this.checked) {
var CandyChoice = "url(" + "http://www.AWebSite.com/Test/Website/Images/" + this.value + ".png )";
$('#Jar4').css("background-image", CandyChoice);
}
}
if (document.getElementById("CandyCount").value == 2) {
$(':checkbox(:checked)')
if (this.checked) {
var CandyChoice = "url(" + "http://www.AWebSite.com/Test/Website/Images/" + this.value + ".png )";
$('#Jar3').css("background-image", CandyChoice);
}
}
if (document.getElementById("CandyCount").value == 1) {
$(':checkbox(:checked)')
if (this.checked) {
var CandyChoice = "url(" + "http://www.AWebSite.com/Test/Website/Images/" + this.value + ".png )";
$('#Jar2').css("background-image", CandyChoice);
}
}
if (document.getElementById("CandyCount").value == 0) {
$(':checkbox(:checked)')
if (this.checked) {
var CandyChoice = "url(" + "http://www.AWebSite.com/Test/Website/Images/" + this.value + ".png )";
$('#Jar1').css("background-image", CandyChoice);
}
}
}
/* *****END***** THIS IS THE SECTION THAT CHANGES THE PLACE HOLDER IMAGE TO THE CHECKBOX IMAGE. ********END*******/
if (n == c)
{
$(':checkbox:not(:checked)').prop('disabled', true);
document.getElementById('AddCart').style.display = 'inline';
$(document).scrollTop( $("#AddCart").offset().top );
$("#StepFourCheck").fadeIn('slow');
}
else
{
$(':checkbox:not(:checked)').prop('disabled', false);
document.getElementById('AddCart').style.display = 'none';
document.getElementById('StepFourCheck').style.display = 'none';
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes, use a different class for each img but for each create one with the checked class
ASKER
thanks! Not to be a pain but would you be able to add one example to your jsfiddle code?
ASKER
I tried to give each checkbox its own id with its own checked class. But no luck. I'm sure I'm doing something wrong. Do I have to change the jQuery as well?
Do you think you can alter your code to show me how each checkbox would associate with its own image when checked. You don't have to do all 6 checkboxes, just a couple so I can get the gist of it.
I really appreciate any help, thanks!
Do you think you can alter your code to show me how each checkbox would associate with its own image when checked. You don't have to do all 6 checkboxes, just a couple so I can get the gist of it.
I really appreciate any help, thanks!
ASKER
I just noticed if there are more checkboxes then placeholder images, then the extra checkboxes does not change the place holder images.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The place holder image will all be the same but each checkbox is associated with its own unique image.