2

I have this simple HTML code:

<div id="new_gallery">
    <p id="add_gallery">Add new gallery</p>
</div>

and jQuery code:

<script>
    $("#add_gallery").click(function() {
        $("#new_gallery").append('<input name"new_gallery" /><a href="#" id="create_new_gallery">Add</a>');
        $(this).remove();
    });

    $("#create_new_gallery").on('click', function(){
        alert('1'); 
    });
</script>

First function is working, but second one is not. I need to create new input element, send data via ajax, and then delete the input element and append a p element once again. How can I do this?

4 Answers 4

6

When the second statement runs, the element #create_new_gallery does not exist yet so it does nothing.

You can do the binding to the click event after you created the element for instance, this ensures the element exists in the DOM:

$("#add_gallery").click(function() {
    $("#new_gallery").append('<input name="new_gallery" /><a href="#" id="create_new_gallery">Add</a>');
    $(this).remove();
    $("#create_new_gallery").on('click', function() {
        alert('1');
    });
});​

DEMO


Here is a little bit more optimized version. It's a bit non-sense to append an element and have to re-query for it (event though querying by id is the fastest method. Besides, it's best to use the chaining capabilities of jQuery afterall:

$("#add_gallery").click(function() {
    var $gallery = $("#new_gallery");

    $('<input name="new_gallery" />').appendTo($gallery);
    $('<a href="#" id="create_new_gallery">Add</a>')
        .on('click', function() {
            alert('1');
        })
        .appendTo($gallery);

    $(this).remove();
});​

DEMO

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

1 Comment

A pity that create_new_gallery has to be created and selected in the same part of the code. Have a look to my answer.
2

#create_new_gallery doesn't exist when you bind its click event.

Here is what your code should look like:

$("#add_gallery").click(function() {
  var newG = $("#new_gallery");
  $('<input name"new_gallery" />').appendTo(newG);
  $('<a href="#" id="create_new_gallery">Add</a>').appendTo(newG).on('click',
    function() {
      alert('1');
    });

  $(this).remove();
});

Notice that getting $("#new_gallery") into a variable avoid to look for it twice.

Comments

1
$(document).ready(function () {
    $("#add_gallery").click(function() {
        $("#new_gallery").append('<input name"new_gallery" /><a href="#" id="create_new_gallery">Add</a>');
        $(this).remove();
        $("#create_new_gallery").on('click', function(){
           alert('1'); 
        });
    });
});

DEMO: http://jsfiddle.net/39E4s/2/

Comments

-1

Try live to handle the events fired for elements added after the page has loaded.

$("#create_new_gallery").live('click', function(){
   alert('1'); 
});

http://api.jquery.com/live/

1 Comment

live() is deprecated. Use delegate() or on() instead.

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.