1

I'm trying to make a script that collapses and expands when you either press the text or the image. The images is a basic arrow, which is being replaced by an arrow pointing upwards when the div is expanded.

My problem is that the text disappears when I toggle the div. How should I write my code, so the headline would stay, no matter if the div is expanded or not? I would like to use the following script on several divs.

Furthermore the slideToggle() function does not work both ways, how do I solve this?

<script type="text/javascript">
        function toggle(divId, switchImgTag) {
            var ele = document.getElementById(divId);
            var imageEle = document.getElementById(switchImgTag);
            $("#" + divId).slideToggle("fast");
            if (ele.style.display == "block") {
                ele.style.display = "none";
                imageEle.innerHTML = '<img src="images/arrow_down.png">';
            }
            else {
                ele.style.display = "block";
                imageEle.innerHTML = '<img src="images/arrow_up.png">';                    
            }
        }
    </script>

And the HTML:

<a id='imageDivLink' href="javascript:toggle('skills', 'imageDivLink');">
     <h2>Skills And Expertise</h2>
     <img class='arrow' src='images/arrow_down.png'>
</a>
<div id='skills' style='display:none;'>
     Lorem Ipsum...
</div>

4 Answers 4

2

Try

function toggle(divId, switchImgTag) {
    var ele = $("#" + divId);
    var imageEle = $('#' + switchImgTag).find('img');
    if (ele.is(':visible')) {
        imageEle.attr('src', "images/arrow_down.png");
    } else {
        imageEle.attr('src', "images/arrow_up.png");
    }
    $("#" + divId).slideToggle("fast");
}

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

First of all $("#" + divId).slideToggle("fast"); in your case is useless, since you do the toggle manually using simple jquery right after it. Read http://api.jquery.com/slideToggle/ and after you understand it you can just remove everything from that point on.

Also use the "complete" callback to add your "personal touches" at the end of animation.

Your function should look like this:

    function toggle(divId, switchImgTag) {
        var ele = document.getElementById(divId);
        var imageEle = document.getElementById(switchImgTag);
        $("#" + divId).slideToggle('fast', function() {
          // Change icon.
          // Change text.
        });
    }

About the text, you can change it the same way you currently change image.

Comments

0

innerHTML replace the whole element(affects it child elements).

So work on the img tag like below

if (ele.style.display == "block") {
                ele.style.display = "none";
                $('.arrow').prop('src', 'images/arrow_down.png'); //change like

            }
            else {
                ele.style.display = "block";
                $('.arrow').prop('src', 'images/arrow_up.png');  //change like             
            }

Comments

0

Problem is - you're using innerHTML it replaces all of your element HTML, to avoid it use append();

Comments

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.