Making JQuery horizontal accordion close on click

Example: http://vincent-massaro.com/map/

Currently, the script allows you to click on a piece of the accordion to open it, but it is set to close on mouseleave. When I set the mouseleave to .click, it gets confused and doesn't know what state it is in. I want to make it so that you can click to open it, and click to close it, instead of mouseleave. The code controlling this is below, and the full script is in haccordion.js linked in the page source. If someone could help me modify this script, I would be very grateful! Thanks in advance.
$target.click(function(){
            haccordion.expandli(config.accordionid, this)
                    config.$lastexpanded=$(this)
  })
            if (config.collapsecurrent){ //if previous content should be contracted when expanding current
                    $target.mouseleave(function(){
                          $(this).stop().animate({width:config.paneldimensions.peekw}, config.speed)
  })
  }

Open in new window

vmassaroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StealthyDevCommented:
keep a variable to hold the status of the according.
so that if the status is open, call the close according (which is now in mouse-leave).

ie, handle both in click, use a if-condition.
0
vmassaroAuthor Commented:
Thanks for the response. I am a JQuery and Javascript beginner. Can you show me an example of how I would change this code?
0
StealthyDevCommented:
I think, this may help you!



$target.click(function(){
	if(config.$lastOpen == $target)
	{
		$(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
	}
	else
	{
		haccordion.expandli(config.accordionid, this);
		config.$lastexpanded = $(this)
		config.$lastOpen = $target
	}
})
/*if (config.collapsecurrent){ //if previous content should be contracted when expanding current
	    $target.mouseleave(function(){
		  $(this).stop().animate({width:config.paneldimensions.peekw}, config.speed)
	})
}*/

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

vmassaroAuthor Commented:
This script change seems to work aside from closing the accordion. If you click a point on the map, it opens the accordion, but clicking the accordion to close it does not always close it the first time, and sometimes you need to click it twice.

Example: http://vincent-massaro.com/map/
0
StealthyDevCommented:
Okay, you have window-1, window-2 "points in map" na, in the click event of that maps, you are calling the expandli function. add the below content to it:

config.$lastexpanded = <target list>
config.$lastOpen = <target list>

Regards.
0
vmassaroAuthor Commented:
Sorry I don't understand. Can you show what would go in <target list>?
0
StealthyDevCommented:
can you send me the click event of the POINT-MAPs in the map?
0
vmassaroAuthor Commented:
Sure, here is the code for the click event for a marker:
            
GEvent.addListener(marker, 'click', function(){
haccordion.expandli('slices', 0);
});

You can see this also by viewing the source on that page.
0
StealthyDevCommented:
I think, you are giving me the click event of marker-1, and 0 is the id of the first according?

if so, use the below code and give the same for other markers also.


GEvent.addListener(marker, 'click', function(){
haccordion.expandli('slices', 0);
config.$lastexpanded = 0
config.$lastOpen = 0
});

Open in new window

0
StealthyDevCommented:
ie, config.$lastexpanded = 1 for the second marker, and so, and so...
0
vmassaroAuthor Commented:
I get an error in Firefox console from this code:

Error: config is not defined
Source File: http://vincent-massaro.com/map/
Line: 124

The click to close appears to work fine when clicking the accordion slices themselves, just not when a marker is clicked on the map. Please take a look at my example as I have updated it with your suggestion. Thanks!
0
StealthyDevCommented:
Ok.. Change your haccordion.js as below:

add this in the top of haccordion.js
var isMarker = false;


do the below changes:

            GEvent.addListener(marker, 'click', function(){
                  haccordion.expandli('slices', 0);
                  isMarker = true;
            });

            GEvent.addListener(marker2, 'click', function(){
                  haccordion.expandli('slices', 1);
                  isMarker = true;
            });

i mean, for all the markers.


$target.click(function(){
	if(isMarker)
	{
		$(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
	}
	else
	{
		if(config.$lastOpen == $target)
		{
			$(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
		}
		else
		{	
			haccordion.expandli(config.accordionid, this);
			config.$lastexpanded = $(this)
			config.$lastOpen = $target
		}
	}
})

Open in new window

0
StealthyDevCommented:
hi, i forgot to make isMarker to false...


$target.click(function(){
	if(isMarker)
	{
		$(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
		isMarker = false;
	}
	else
	{
		if(config.$lastOpen == $target)
		{
			$(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) //contract previous content
		}
		else
		{	
			haccordion.expandli(config.accordionid, this);
			config.$lastexpanded = $(this)
			config.$lastOpen = $target
		}
	}
})

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vmassaroAuthor Commented:
That did it! Thank you for your help and persistence!
0
StealthyDevCommented:
Glad to hear that the script worked!
You are always welcomed.

Best Regards.
0
vmassaroAuthor Commented:
I just noticed one slight issue. When clicking on an image slice, and then clicking to close it, it is not possible to click and open the same slice without clicking and opening a different slice first. You can test this in the demo: http://vincent-massaro.com/map/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.