2

I'm simply trying to change a hover trigger to a click but for some reason any click function I use does not work?

This my current code that works when you hover...

Current Script Thats Working on hover...

    $showSidebar.hover(function() {
        $wrapper.stop().animate({ left: "-178px" }, 300);
        $sidebarSlider.stop().animate({ width: "452px" }, 300);
        $latestTweet.stop().animate({width: "452px", top : "-1px", backgroundColor: "#464848" }, 300);              
    }, function() {
        $wrapper.stop().animate({ left: "0" }, 300);
        $sidebarSlider.stop().animate({ width: "274px" }, 300);
        $latestTweet.stop().animate({ top : "-1px", width: "274px", backgroundColor: "#464848" }, 300);
    });

But see my attempts below to get it work when clicked, but weirdly nothing happens.

My Attempt One

    $showSidebar.click(function() {
        $wrapper.stop().animate({ left: "-178px" }, 300);
        $sidebarSlider.stop().animate({ width: "452px" }, 300);
        $latestTweet.stop().animate({width: "452px", top : "-1px", backgroundColor: "#464848" }, 300);              
    }, function() {
        $wrapper.stop().animate({ left: "0" }, 300);
        $sidebarSlider.stop().animate({ width: "274px" }, 300);
        $latestTweet.stop().animate({ top : "-1px", width: "274px", backgroundColor: "#464848" }, 300);
    });

and

My Attempt Two

    $showSidebar.on('click', function () {
        $wrapper.stop().animate({ left: "-178px" }, 300);
        $sidebarSlider.stop().animate({ width: "452px" }, 300);
        $latestTweet.stop().animate({width: "452px", top : "-1px", backgroundColor: "#464848" }, 300);              
    }, function() {
        $wrapper.stop().animate({ left: "0" }, 300);
        $sidebarSlider.stop().animate({ width: "274px" }, 300);
        $latestTweet.stop().animate({ top : "-1px", width: "274px", backgroundColor: "#464848" }, 300);
    });

and a couple of others but nudda...

This is my mark up...

<a href="#" title="More" class="button blu large" id="show-sidebar"><span>//</span> MORE</a>

and my var

$showSidebar  = $("#show-sidebar");

Any ideas where I'm going wrong? Would be hugely helpful thanks!


See working code, thanks @hurshagrawal

$showSidebar.on('click', function () {
    if ($showSidebar.html() == '<span>//</span> CLOSE') {
        $wrapper.stop().animate({ left: "0" }, 300);
        $sidebarSlider.stop().animate({ width: "274px" }, 300);
        $latestTweet.stop().animate({ top : "-1px", width: "274px", backgroundColor: "#464848" }, 300);
    } else {
        $wrapper.stop().animate({ left: "-178px" }, 300);
        $sidebarSlider.stop().animate({ width: "452px" }, 300);
        $latestTweet.stop().animate({width: "452px", top : "-1px", backgroundColor: "#464848" }, 300);
        $showSidebar.html('<span>//</span> CLOSE');
    }
});
9
  • Using Firebug in FF do you get any javascript errors? Commented Dec 1, 2011 at 22:50
  • 1
    Why do you pass 2 parameters into .click()? Commented Dec 1, 2011 at 22:51
  • Nope, syntax errors, not nothing :-/ Commented Dec 1, 2011 at 22:54
  • @zerkms how do you mean? Commented Dec 1, 2011 at 22:55
  • @Krister Andersson: how about second code sample? Commented Dec 1, 2011 at 22:55

3 Answers 3

3

I think you are looking for the .toggle() command:

$showSidebar.toggle(function() {
    $wrapper.stop().animate({ left: "-178px" }, 300);
    $sidebarSlider.stop().animate({ width: "452px" }, 300);
    $latestTweet.stop().animate({width: "452px", top : "-1px", backgroundColor: "#464848" }, 300);              
}, function() {
    $wrapper.stop().animate({ left: "0" }, 300);
    $sidebarSlider.stop().animate({ width: "274px" }, 300);
    $latestTweet.stop().animate({ top : "-1px", width: "274px", backgroundColor: "#464848" }, 300);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah good call... but one problem, I need to control the second alternation via another script, with toggle, if I click on the a#show-sidebar button when second alternation is run by another script, then it runs the second alternation again instead of starting at the beginning.
Not sure if I understand you correctly; are you saying that there is another script that executes code similar to clicking the sidebar? If that is the case, it is better to pipe that other code into actually clicking the side bar: e.g. function SomeOtherCode() { showSidebar.trigger('click'); }.
1

I don't think $.click() takes 2 functions as parameters. Did you try taking the second function out? If you're trying to toggle between the two, you might have to write something with an if-else in there.

EDIT: Ah, or use .toggle().

1 Comment

Yep, your right, doesn't work like this, need to rewrite it a bit. Cheers
0

Try using bind(), I've found that more reliable across browsers.

http://api.jquery.com/bind/

basic usage in your case would be (untested):

$showSidebar.bind('click', function() {
  // do stuff
});

1 Comment

interesting, will try this out now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.