0

why i click on "close" div, don't show "basic text"? in short, why don't work second "click" function?

<!DOCTYPE html>
    <html>
    <head>  
        <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            $("#sp").click(function(){
                $("#info").html("<div><div id='close'>Close</div><div>new text</div></div>");
            });

            $("#close").click(function(){
                $("#info").html("<div>basic text</div>");
            });
        });
        </script>
        <style>
        </style>
    </head>
    <body>
    <p id="sp">Click</p>
    <div id="info">
        <div>basic text</div>
    </div>
    </body>
    </html>

4 Answers 4

3

You will need the live() function for this:

$("#sp").click(function(){
    $("#info").html("<div><div id='close'>Close</div><div>new text</div></div>");
});

$("#close").live('click', function(){
    $("#info").html("<div>basic text</div>");
});

Your element with the id closed is created after the click event handler has been attached (to nothing in this case). You'll need to ensure that it will be also attached to dynamically created elements. And for this you can use live()

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

Comments

0

Because close doesn't exist when the click is set. Use the live function instead.

$("#close").live("click", function(){
  $("#info").html("<div>basic text</div>");
});

Comments

0

Because the close element is added dynamically, and not present in the DOM on page-load.

If you amend the code a little, to:

$("#close").live('click',
    function(){
        $("#info").html("<div>basic text</div>");
});

It should work fine.

live() allows events to be bound to elements not-yet created/present in the DOM.

Comments

0

Events bubble, so you could just place a handler directly on info that looks for click on elements with the ID close.

$(document).ready(function(){
    $("#sp").click(function(){
        $("#info").html("<div><div id='close'>Close</div><div>new text</div></div>");
    });

    $("#info").click(function(e){
        if( e.target.id === "close" ) {
            $(this).html("<div>basic text</div>");
        }
    });
});

As an alternative, you could use delegate()(docs), which does essentially the same thing.

$(document).ready(function(){
    $("#sp").click(function(){
        $("#info").html("<div><div id='close'>Close</div><div>new text</div></div>");
    });

    $("#info").delegate('#close','click',function(e){
        $(this).parent().html("<div>basic text</div>");
    });
});

Ultimately the best solution would be to have all the markup on the page when it loads, and just show()(docs) and hide()(docs) it as necessary. This is much better than destroying and recreating reusable content.

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.