1

I have a link that, when clicked expands into a div below. I need to add some sort of indicator to "My Link", like a "carrot" icon that will turn down to indicate expanding of the section. Basically I thought I need to add the icon in a css class and then swap that class with another that has different image. I found toggleClass bit it seems like it adds/removes the class instead of swapping it.

So how would I replace class?

Here's what I have now without adding the visual indicator.

$("#mLink").click(function() {
    $("#showHideDiv").slideToggle("fast");
});

<span id="mLink">My Link</span>
<div id="showHideDiv">
   hidden content
</div>

I

2 Answers 2

3

You should invoke .toggle()help. Like

$('#myLink').toggle(function() {
    $("#showHideDiv").slideDown("fast").toggleClass('open');
}, function() {
    $("#showHideDiv").slideUp("fast").toggleClass('open');
});
Sign up to request clarification or add additional context in comments.

Comments

2

Just string 2 toggles together something like

$('#myDiv').toggleClass('a').toggleClass('b');

and have your target start with one of the classes..

<div id="myDiv" class="b"> bla bla </div>

and then each call will toggle both giving the result you want.

1 Comment

+1 IMO this is the best way - simple and uncomplicated. Works perfectly

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.