0

I have this code

$("div").click(function () {
    var CurrId = $(this).attr('id');
    if (CurrId == "First") {
        $("#MenuContent").empty();
        $("#MenuContent").hide();
        $('<img src="images/End_05.png" width="30" height="30"  /> <a id="First"  class style="text-decoration:none;color:black">any </a>').appendTo("#MenuContent");
        $("#MenuContent").show("slow");
    }
});

I.e when I click on div an image (suppose its name is mm) and a link appear. Now I wrote this:

$("img").click(function () {
    alert('yes');
});

When I click on image (that mm) nothing is displayed. Any ideas?

2 Answers 2

4

$('img') doesn't account for elements created after it was called. You have to use event delegation to account for them:

$('#MenuContent').on('click', 'img', function () {
    alert('yes');
});
Sign up to request clarification or add additional context in comments.

Comments

1

Possibly you are calling the second function before the image was actually added. Try using
jquery.live(); or jQuery.on() or jQuery.delegate() not click().

Note: jQuery.live() has been deprecated.

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.