1

I have a div that show 200 px. When I click on a image in the div I want to show 1000px. Then close to 200px again when clicked once again.

This is my code:

<script type="text/javascript">
$(document).ready(function() {
    // shows the slickbox DIV on clicking the link with an ID of "slick-show"

$('#slickbox').css("height", "200px");      
$('#slick-show').click(function() {
        $('#slickbox').toggle(function () {
    $("#slickbox").css("height", "1000px");
}, function () {
    $("#slickbox").css("height", "200px");
      });
});
return false;

});
</script>

<div id="slickbox" style="height: 1000px;">
<p style="text-align: center;">
<a id="slick-show" href="#"><img src="/ImageVaultFiles/id_468115/cf_69211/Happy_monday14-5.jpg" border="0" alt="Happy_monday14-5" /></a></p>
</div>

The code work but I have to doubleclick every time after the first opening.

1
  • check out this Question on stackoverflow, may be this help you. Commented May 11, 2012 at 13:45

5 Answers 5

3

this becuase you attach toggle function after click on element, avoid .click:

   $(function() {
        // shows the slickbox DIV on clicking the link with an ID of "slick-show"

    $('#slickbox').css("height", "200px");      
    $('#slick-show').toggle(function () {
        $("#slickbox").css("height", "1000px");
    }, function () {
        $("#slickbox").css("height", "200px");
          });
    });
Sign up to request clarification or add additional context in comments.

Comments

2

You are misusing toggle.

http://api.jquery.com/toggle-event/

Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks.

toggle should only be called once. It will bind click handlers which will trigger on alternating clicks. What you are doing is re-binding click handlers every time you click. What you really want to do is something like this:

$('#slickbox').css("height", "200px");      
$('#slick-show').toggle(function () {
    $("#slickbox").css("height", "1000px");
}, function () {
    $("#slickbox").css("height", "200px");
});

Comments

1

The .click() and .toggle() are redundant -- .toggle() assumes someone's going to be clicking on an element. See description in docs: "Bind two or more handlers to the matched elements, to be executed on alternate clicks." Just leave out the .click() and you'll get the behavior you want..

$(document).ready(function() {
    $('#slickbox').css("height", "200px");   
    $('#slick-show').toggle(function() {
        $("#slickbox").css("height", "1000px");
    }, function() {
            $("#slickbox").css("height", "200px");
      });   
})​

Comments

0

Use CSS class for different styles or widths or heights or anything

and then use

http://api.jquery.com/toggleClass/

for toggling any style

1 Comment

We are locked to a useless cms that makes is a pain in the a** to use classes in javascripting.
0

Yup, your toggle function is incorrect. Here's a working example:

http://jsfiddle.net/LsdUQ/

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.