0

I have a jfiddle below that has two links that expand two divs independently and horizontally. http://jsfiddle.net/j9W7R/

How would I go about causing only one div to be seen any any given time? Say you click the first one, then click the second and it causes the first to contract. Or if you click the first and click the first again it would also retract.

The idea is that the link will be a Facebook icon, the next link will be another social media icon, etc. etc. and I will embed a little "like us" or some sort of interaction with user.

LITguy
  • 623
  • 4
  • 13
  • 39
  • Display one at a time. Yes if div is clicked again it will retract and go back where it came from. Like accordion scripts do but horizontally. Samples below are great and I will try it now, but I may have 3, 4, or 5 icons to click eventually so these 2 are just a start. – LITguy Aug 14 '12 at 11:40

1 Answers1

1

You can do it next way:

To all your sliding <div>s add some class. class="slickbox" for example.

And each .click() start with $('.slickbox').hide(); hiding all boxes first and than expanding the one you need.

For e.g.:

<div style="clear:both;">

  <div style="float:left;">
    <a href="#" id="slick-slidetoggle2">Slide toggle the box 2</a>
  </div>

  <div id="slickbox2" class="slickbox">
    2: This is the box that will be shown and hidden and toggled at your whim. :)
  </div>

</div>

And one of clicks will look like this.

$('#slick-slidetoggle2').click(function() {
    $('.slickbox').hide(); // hide all boxes (doesn't matter if they were opened or not)
    $('#slickbox2').show('slide', 400); // and open one that was clicked
    return false;
});

This way you can extend your Like Us easily adding new items without writing .hide() for each opened/closed item.

UPDATE: And if you need to close item if user again clicks on it, you can change show/hide section of the button .click() handler to the following.

$('#slick-slidetoggle2').click(function() {

    if ( $('#slickbox2').is(':hidden') ) { // check if current box is hidden
        $('.slickbox').hide(); // hide all boxes (doesn't matter if they were opened or not)
        $('#slickbox2').show('slide', 400); // and open this one
    } else // if it was already opened
        $('#slickbox2').hide('slide', 400); // hiding the box

    return false;
});

:hidden selector will track the state of object visibility and basing on this you are deciding what to do with the box.

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
  • Ok I will try this. Very well described! – LITguy Aug 14 '12 at 11:51
  • I got the first part to work but I can't seem to find where to input the UPDATE you pasted. Yes, I will need item to close after clicks on it. It makes sense and I got it to work separately but not sure how to combine. Do I paste the updated code WITH the first part or replace the first part? http://jsfiddle.net/j9W7R/8/ – LITguy Aug 14 '12 at 12:09
  • @LITguy, updated with full example for closing box if it was opened. – Paul T. Rawkeen Aug 14 '12 at 12:14
  • I could be mistaken but it's not working correctly. It doesn't seem to close and its maybe because of the first $('.slickbox').hide(); script causing it to jump closed instead of allowing for a click to contract? – LITguy Aug 14 '12 at 13:02
  • 1
    @LITguy, Yeah. My mistake. Sorry. I've update the code. Try if its working as it should. Don't have enough time to check right now. – Paul T. Rawkeen Aug 14 '12 at 14:40
  • That did it! Thank you for the great follow up. I see the order you changed things and it makes sense to me now. I appreciate the lesson! – LITguy Aug 15 '12 at 06:47
  • Well, I am at a loss for words. It works amazing in jsfiddle but I still haven't been able to get it to work on a basic blank html test page. I literally pasted the css in the head, script also in head, and divs in the body. All the css and scripts have proper surrounding tags. Not sure why something can work in jsfiddle but not on a new page. I even included latest jquery supporting link. – LITguy Aug 16 '12 at 09:19