Avatar of Al4ddin2
Al4ddin2 asked on

Check if div is hidden to decide whether to slideDown or slideUp

Hi,

I currently have this script which slides down hidde content when a trigger is clicked, it also changes the text from 'More' to 'Less' and the image from a down arrow to an up arrow.
Firstly is there a more efficient way of doing it and also I need to add an action so that if the content is now shown and the text and image changed then the next time a user clicks it slides Up.

function toggleslideDown (activator, selector) {
				$(activator).click(function(){
					$(this).prev('section').find(selector).slideDown();
					$(this).find('span').text("Less");
					$(this).children('img').attr('src', 'img/up-arrow.png');
				});
			}

Open in new window


Thanks
jQuery

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
Rob

I think you're just looking for an accordion, such as this one?
http://jqueryui.com/accordion/
That should make it easier
leakim971

your code updated :
function toggleslideDown(activator, selector) {
    $(activator).click(function(){
        var text = $(this).find('span').text();
        var b = text == "Less";
        $(this).find('span').text(b?"More":"Less");
        if(b) $(this).prev('section').find(selector).slideDown(); else $(this).prev('section').find(selector).slideUp();
        $(this).children('img').attr('src', b:'img/down-arrow.png'?'img/up-arrow.png');
    });
}

Open in new window

ASKER
Al4ddin2

Thanks leakim971,
I have tweaked it slightly but I am getting an error on this line:

$(this).children('img').attr('src', b:'img/down-arrow.png'?'img/up-arrow.png'); 

Open in new window


Error: SyntaxError: missing ) after argument list
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
leakim971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question