1

I have been working on this for a bit now and just can't seem to get it to work. Where my problem is is that the css class is not being removed.

jQuery$(document).ready(function(){
    $('#about').click(function(){
        $('#about').addClass('bookOpen').before('<a href="#" class="closeButton">Close</a>');
    });

    $('.closeButton').click(function(){
        $(this).next().removeClass('bookOpen');         
    });
});
1
  • 1
    Have you tried .live()? Try calling each click this way: $('#about').live("click",function() { Commented Mar 2, 2011 at 21:19

4 Answers 4

4

You need to use .live instead of .click since you are dynamically adding DOM elements.

$('.closeButton').live('click', function() {
    $(this).next().removeClass('bookOpen'); 
});
Sign up to request clarification or add additional context in comments.

Comments

2

Use live():

$(document).ready(function() {
    $('#about').click(function() {
        $('#about').addClass('bookOpen').before(
            '<a href="#" class="closeButton">Close</a>');
    });
    $('.closeButton').live('click', function() {
        $(this).next().removeClass('bookOpen');
    });
});

Comments

1

Use live

jQuery$(document).ready(function(){
    $('#about').click(function(){
        $('#about').addClass('bookOpen').before('<a href="#" class="closeButton">Close</a>');
    });

    $('.closeButton').live('click', function(){
        $(this).next().removeClass('bookOpen');         
    });
});

Comments

0

When you dynamically add something to the DOM, you need to re-add any jquery triggers to the new piece of DOM.

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.