views:

554

answers:

2

Hi I am trying to do a tri state rollover using jquery. I want an on state off state and click state.

I have to replicate a specific interface style and not using images in not an option. Below is the code that I have used. However, I cannot seem to figure out how deactivate an image when it another is clicked.

Here is a the scenerio.

1) User hovers over image which activates the on state image. 2) User moves off that image and on to another image. THe previous active image is turned off and the the image that is currently hovered over is on. 3) The user clicks an image which activates the clicked state image however, when the user clicks another image previously clicked image goes back to the off state.

I am able to do this with Javascript however, I am new to Jquery and having a hard time trying to figure this out.

Here is the code:

$(document).ready(function() {
   // Navigation rollovers
   $("#nav a").mouseover(function(){
        imgsrc = $(this).children("img").attr("src");
        matches = imgsrc.match(/_on/);
        // don't do the rollover if state is already ON
        if (!matches) {
           imgsrcON = imgsrc.replace(/.gif$/ig,"_on.gif");
           // strip off extension
           $(this).children("img").attr("src", imgsrcON);
        }
    });
    $("#nav a").mouseout(function(){
        $(this).children("img").attr("src", imgsrc);
    });
    // Navigation clicks
    $("#nav a").click(function(){
        imgsrc = $(this).children("img").attr("src");
        clkmatches = imgsrc.match(/_on/);
        // don't do the rollover if state is already ON
        if (!clkmatches) {
            imgsrcCLK = imgsrc.replace(/.gif$/ig,"_clk.gif");
            // strip off extension
            $(this).attr("src", imgsrcCLK);
        }
    });  
});
A: 

To clarify, if an image has been clicked, you want it to stay in the clicked state even if another image is clicked?

If this is the case, I would add a class when the image is clicked, then in your check to remove the state, do a check to see if that class is on the image.

ex.

$('item').click(function(){$(this).addClass('clicked');}
$('other_item').click(function(){
     $('all_items') -- if class != 'clicked' 
         { go ahead and revert it to a normal state, otherwise don't touch it}

Hopefully my pseudo code makes sense.

idrumgood
Thanks, Actually what I want to do is have it work like a tab control. Moving over the image envokes the on state. Move off the image envokes the off state and clicking on the image envokes the click state. However, if I go and click another image it then deactivates the click state of the clicked image and activates the newley clicked image to the click state. Does that make more sense?Thanks
Rob
Gotcha. Well, after reading that, and the answer below mine, he's got the right idea, so I'd say just go with Cide's answer.
idrumgood
Thanks I appreciate the help.
Rob
+1  A: 

This piece of code should accomplish what you're looking for. While I haven't tested its correctness (since I presently should be working!), it should at the very least illustrate a way of doing this.

var clicked_obj;
$("#nav a").mouseover(function() {
    if ( $(this).data("clicked") ) { return; }
    $(this).children("img").each(function() {
        this.src = "<path to mouseover image>";
    });
}).mouseout(function() {
    if ( $(this).data("clicked") ) { return; }
    $(this).children("img").each(function() {
        this.src = "<path to original image>";
    });
}).click(function() {
    if ( clicked_obj ) {
        $(clicked_obj).removeData("clicked").mouseout();
    }
    clicked_obj = this;
    $(this).data("clicked", true);
    $(this).children("img").each(function() {
        this.src = "<path to clicked image>";
    });
});
Cide
Thanks. When I try to run the above I get the following error. clicked_obj is undefined.
Rob
ah, I forgot to prepend a "var clicked_obj;". I'll correct it.
Cide
Ahh thanks, I actually figured it out last night. I appreciate your help with this.
Rob
Cide:I have one more question. The code above assumes that the clicked image is the same for each image. In my case I am using this in a nav bar. So each image needs to be swapped out based on the image that is being clicked. So I have three images for each tab. I have the following. nav_acct_off.gifnav_acct_clk.gifnav_acct_on.gifI need to make a generic function so that I can replace the _click, _off and _on regardless of the image Something like $(this).attr("src").replace('_off', '_on'); However this does not work because I still have to change the src for clicked.
Rob
You can just do:this.src = this.rc.replace("_off", "on");
Cide
Thanks for the help. This issues I am having now is I cannot seem to get the active click state to deativate the othe buttons. When I click on button 1 and activate it I need to deativate any other active buttons. Here is the code that you sent modified a but to make it more generic.
Rob
Sorry the code is more then 600 chars to post in a comment. Can I email it to you?
Rob