1
jQuery(document).ready(function () {
    jQuery('.isSocial').click(function () {
        var hidden = jQuery('.socialHidden');
        hidden.animate({
            "top": "35px"
        }, 400);
        jQuery(".socialHidden>div").delay(400).animate({
            opacity: "1"
        }, 150);
        jQuery(".isSocial").addClass("closejs").removeClass("isSocial");
        return false;
    });
    jQuery('.closejs').click(function () {
        var hidden = jQuery('.socialHidden');
        hidden.delay(200).animate({
            "top": "-176px"
        }, 400);
        jQuery(".socialHidden>div").animate({
            opacity: "0"
        }, 150);
        jQuery(".closejs").addClass("isSocial").removeClass("closejs");
    });
});

Can anyone help me fix this code? The first part works perfectly... When I try to get the second one working, it just does nothing.

This is the html:

    <button class="isSocial">BUTTON</button>
    <div class="socialHidden">
    <div>
    content.....
    </div>
    </div>

1 Answer 1

3

Since you are changing the classes dynamically, you should use event delegation for binding events,

jQuery(document).ready(function () {
    jQuery(document).on("click", '.isSocial', function () {
        var hidden = jQuery('.socialHidden');
        hidden.animate({
            "top": "35px"
        }, 400);
        jQuery(".socialHidden>div").delay(400).animate({
            opacity: "1"
        }, 150);
        jQuery(".isSocial").addClass("closejs").removeClass("isSocial");
        return false;
    });
    jQuery(document).on("click", '.closejs', function () {
        var hidden = jQuery('.socialHidden');
        hidden.delay(200).animate({
            "top": "-176px"
        }, 400);
        jQuery(".socialHidden>div").animate({
            opacity: "0"
        }, 150);
        jQuery(".closejs").addClass("isSocial").removeClass("closejs");
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1. To further expand, whenever you have dynamic elements (elements that are added by the user, or elements that change classes like above), the code that you wrote will not affect those elements, unless you delegate event handlers to parent elements (in this case, document is the explicit parent). This is because of the way event bubbling works (goes up then down, or down then up, through the DOM, depending on how you define your event listeners).
@JoshBeam thats a nice explanaion.

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.