views:

112

answers:

2

Hi

Basically I have 4 images and 4 button. All 4 images are black and white.

Button1 click -- > toggles image 1 from black and white to color image
Button2 click -- > same for image2
Button3 click -- > same for image3
Button4 click -- > same for image4

So far my code is partially working. Problem is that when I click button 1 and then click button 2....at that point both image1 and image2 will stay as color images.

What I would like to do is that on any button click check to see if any other images are toggled as color images , if so then toggle them back to black and white and only toggle selected images to color image.

Button2 click -- > First: check to see if any images toggled as color iamges , if so toggle them back
second:toggle image2 from black and white to color images

Code

<button id="btn1" >Toggle1</button>
<button id="btn2" >Toggle2</button>
<button id="btn3" >Toggle3</button>
<button id="btn4" >Toggle4</button>

<div class="div1" ><img src="graphics/image1_black.jpg" /></div> <div class="div1 divblack" style="display: none"><img src="graphics/image1.jpg" /></div>

<div class="div2" ><img src="graphics/image2_black.jpg" /></div> <div class="div2 divblack" style="display: none"><img src="graphics/image2.jpg" /></div>

<div class="div3" ><img src="graphics/iamge3_black.jpg" /></div> <div class="div3 divblack" style="display: none"><img src="graphics/image3.jpg" /></div>

<div class="div4" ><img src="graphics/iamge4_black.jpg" /></div> <div class="div4 divblack" style="display: none"><img src="graphics/iamge4.jpg" /></div>

<script>

$("#btn1").click(function() {
    $(".div1").toggle();
});
$("#btn2").click(function() {
    $(".div2").toggle();
});
$("#btn3").click(function() {     
    $(".div3").toggle();
});
$("#btn4").click(function() { 
    $(".div4").toggle();
});            

</script>

=========================================================================

UPDATED CODE:

This is what I am trying to do. As you can see my class overlap between images. So one image can have more than 1 class. This is causing buttons to toggle already toggeled images.

<button id="btn1" >Toggle1</button>
<button id="btn2" >Toggle2</button>
<button id="btn3" >Toggle3</button>
<button id="btn4" >Toggle4</button>

<img class="im1" src="image1_bw.jpg" />
<img class="im1 im2" src="image2_bw.jpg" />
<img class="im2 im3" src="image3_bw.jpg" />
<img class="im4" src="image4_bw.jpg" />

<script>

            // Use the same handler for all button elements
            //   where the ID starts with "btn"
    $("[id^=btn]").click(function() {

              // Grab the number from the end of the ID
        var number = this.id.match(/\d+$/)[0];

              // Find the image ID ending in the same number
              //   and modify its src, toggling the "_black" part of it
        var $img = $(".im" + number).attr('src', function(i, attr) {
            return /_bw/.test(attr) ? attr.replace(/_bw/, '') : attr.replace(/.jpg/, '_bw.jpg');
        });

              // Get the rest of elements with IDs starting with "img"
              //    and modify their src, removing "_black" 
        $("[id^=img]").not($img).attr('src', function(i, attr) {
            return attr.replace('_bw', '');
        });
    });          

</script>
A: 

Just toggle isn't gonna cut it. you need a specific switchToBW and a switchToColor function for that.

The easiest way is: On click you'd toggle all images to b/w, and then just the one you want to color.

A "better" way is: On click switch all those images that are not b/w to b/w and then the one you want to color.

Kris
Isn't this the jQuery way?
unomi
@unomi: wether you use jquery, prototype or plain vanilla javascript makes no difference to the stuff that needs to happen. I don't see what you mean?
Kris
" On click switch all those images that are not b/w to b/w and then the one you want to color." ....How do I detect here that which images are not b/w
Ved
You'd have to look into the implementation of the current `toggle()` function for that answer, since I don't have that, I don't know. give me that function and I'll write up an example in about 6hrs when I get out of bed.
Kris
+2  A: 

Mind if I suggest a different approach?

Just set up one handler for all the buttons, and give the images IDs that end in the same respective number.

Then simply modify the src attribute of the image with the same number to toggle the _black portion, and remove the _black portion of the src from the others.

HTML

<button id="btn1" >Toggle1</button>
<button id="btn2" >Toggle2</button>
<button id="btn3" >Toggle3</button>
<button id="btn4" >Toggle4</button>

<img class="im1" src="image1_bw.jpg" />
<img class="im1 im2" src="image2_bw.jpg" />
<img class="im2 im3" src="image3_bw.jpg" />
<img class="im4" src="image4_bw.jpg" />

jQuery

$("[id^=btn]").click(function () {

    var number = this.id.match(/\d+$/)[0];
    var $img = $(".im" + number);
    var button = this;

    $img.each(function () {
        var $th = $(this);

        if ($th.is('[src*=_bw]')) {
            $th.data('button', button.id);
            $th.attr('src', function (i, attr) {
                return attr.replace(/_bw/, '');
            });
        } else if ($th.data('button') == button.id) {
            if ($th.not('[src*=_bw]').length) {
                $th.attr('src', function (i, attr) {
                    $th.data('button', null);
                    return attr.replace(/.jpg/, '_bw.jpg');
                });
            }
        }
    });
});

EDIT: Updated so that only the button that made an image color can bring it back to black/white.

EDIT: It seems that we have several possibilities to consider given the images associated with a particular button.

The code written will need to incorporate sufficient logic for the following possible scenarios:

The button clicked has...

  1. ...2 images that are b/w, so set to color and store which button set them.
  2. ...2 images that are color, that were set by the same button that was clicked, so set them to b/w and erase the record of the button that made them color.
  3. ...2 images that are color, but both set by different buttons, so the button clicked will have no effect.
  4. ...2 images that are color, but only one of them was set by the button clicked, so make that one image b/w and erase the record of the button that made it color.
  5. ...2 images (1 color and 1 b/w), and the one that is color was set by the button clicked, so either change the color one to b/w and erase the record of the button that made it color, OR make the b/w one color, and store which button made it color. (Not sure which you want.)
  6. ...2 images (1 color and 1 b/w), and the one that is color was set by a different button, so do not change the color one, but change the b/w one to color, and store which button made it color.
  7. ...1 image that is b/w, so set to color and store which button set it.
  8. ...1 image that is color, and was set by the same button that was clicked, so set it to b/w and erase the record of the button that made it color.
  9. ...1 image that is color, and was set by a different button, so the button clicked will have no effect.
patrick dw
this seems to me to be a good solution, I upvoted this instead of writing code in the morning.
Kris
this looks very interesting , let me try this out. I will give u feedback in some time. thanks
Ved
Ok so I tried this approach. It surely decreased my code however I am still not achieving what I wanted.I used your code but I replace ID number with Class so like this<img class="im1" src="image1_bw.jpg" /><img class="im1 im2" src="image2_bw.jpg" /><img class="im2 im3" src="image3_bw.jpg" /><img class="im4" src="image4_bw.jpg" />
Ved
no as you can see second image has two class im1 and im2. if i click on button1 then both images toggle fine. If i click on button2 then second image will toggle back to black and white because it was already toggled by button1 and 3rd image will toggle to color because it is using class im2.Hope I didnt create confusion.thanks
Ved
@Ved - Yes, if you change your requirements, the solution will need to change. It sounds like you want button1 and button2 to toggle the same pair of images. Is that right? I'll need to see your actual HTML and know what you ultimately want to achieve in order to suggest a proper solution.
patrick dw
Hi, thanks for the comment. Actually sorry if i confused you but my original question was like that. You are right i would need to have similar images toggle on and off from more then 1 button....by the way again u r solution was very good for 1 image.
Ved
@Ved - There would be different ways to approach this. First, I need to verify that if `button2` controls `im1 im2`, and if one of those images is `_bw` and the other is not, then how will it decide what to do? For example, is there different behavior if the first one is `_bw` and the second is not, than there is if the second is `_bw` and the first is not? Which image gets priority? Also, will the sequence always be consistent? In other words, will it always be `btn1 -> im1`, `btn2 -> im1,im2`, `btn3 -> im2,im3`, and so on...?
patrick dw
Patrick , Basically if any button toggles any image and make it color then other button should not turn it back to black and white. It should skip it. Let me copy paste my code above.
Ved
patrick dw
...perhaps you could tell me what the overall effect is that you are trying to achieve. I'm really not grasping the end product at this point.
patrick dw
in your example if btn2 turns im2 and im3 to color then on button 3 im3 should stay as color...it wont touch im2 because it is not assigned to im2 so im2 and im3 will stay color....so it is like on every toggle button click check to see if the image it is trying to toggle to color is not alreay color image....does that make sense ?
Ved
@Ved - Thanks. Makes sense now. For some reason I thought that the siblings of the images associated with the button clicked were to revert to `_bw`. I'll update my answer in a minute.
patrick dw
...so if button2 made image3 color, then *only* button2 can bring it back. Button3 will have no impact on image3 until button2 brings it back to black/white. If that's the case, each image needs to remember which button clicked it. I'll update my answer with that in mind. Tell me if I'm wrong.
patrick dw
...I updated my answer. Just be aware that an image can only be changed back to `_bw` by the button that made it color. So if you click button1 then button3, button2 will not have any effect because image2 was made color by button1 and image3 was made color by button3.
patrick dw
@patrick that was amazing effort on your end. I really appreciate your help. There is one glitch though. If you follow these actionsclick button2 > click button1 > click button2 again > click button1 again...on last button click it will turn img1 black but turn img2 color...
Ved
@Ved - I updated my answer with a list of 9 possible scenarios that I could think of for the click of a button. Number 5 has 2 possible outcomes, not sure which one you want. The logic will need to provide for each scenario. At this point, I'm not sure which direction to go with it. Is this the actual code, or will there be more buttons/images? Have a look at my update, and let me know if it seems accurate to you. Also, let me know about #5. I'll take another look tomorrow. :o)
patrick dw