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
Avatar of Rob
Rob
Flag of Australia image

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

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

Avatar of Al4ddin2
Al4ddin2

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
jQuery
jQuery

jQuery (Core) is a cross-browser JavaScript library that provides abstractions for common client-side tasks such as Document Object Model (DOM) traversal, DOM manipulation, event handling, animation and Ajax. jQuery also provides a platform to create plugins that extend jQuery's capabilities beyond those already provided by the library.

19K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo