4

I have a quick jQuery question:

First, my HTML:

<div id="taskinput">
    <form>
         <input id="taskinput-box" type="text"></input>
         <input type="submit"></input>
    </form>
</div><!-- end taskinput -->
<div id="taskoutput"></div><!-- end taskoutput -->

I am creating content in the #taskoutput from user using .append while creating a new class, .task.

$("#taskinput").submit(function() {
    var tasktext = $('#taskinput-box').val()
    $('#taskoutput').append('<div class="task">'+tasktext+'</div>');
    $('#taskinput-box').val("");
    return false;
});

The above code works fine; the trouble arises when I try to select the created divs. The below code does not work:

$(".task").click(function() {
    $(this).slideUp();
});

What am I missing?

steve-er-rino
  • 385
  • 1
  • 3
  • 13

4 Answers4

2

You have to use a delegate event like with on:

$('#taskoutput').on('click', '.task', function(){
    $(this).slideUp();    
});

@salek DEMO but with on

Read the on docs:

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
2

$(".task").click(... says "find all the .task elements that currently exist, and bind an event handler to them. If elements are created later, as in your example, they will not have the event handler bound.

You need to bind the event handler to an element that will always exist. Since ancestor elements are notified of events on their descendants, clicks on .task elements will always trigger click handlers on #taskoutput. jQuery's on method makes this simple for you:

$('#taskoutput').on('click', '.task', function(){
    $(this).slideUp();    
});
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • I am in a similar situation, and tried your solution. But I get an uncaught exception "object does not have methond 'on'". Any ideas? – nom-mon-ir Jan 02 '13 at 04:27
  • Never mind, from http://stackoverflow.com/questions/2124744/jquery-remove-parent-not-working-on-dynamically-created-divs I found a soultion that works. Namely, I had to use .live('change', function() – nom-mon-ir Jan 02 '13 at 05:36
0

Use jquery .on() to bind events on HTML that's generated at runtime. Like this:

$("#taskoutput").on({
    click: function() { $(this).slideUp(); }
}, '.task');
frenchie
  • 51,731
  • 109
  • 304
  • 510
0

what you need to use is live('click', function() { ...

I created a jsfiddle for you at http://jsfiddle.net/2ZmF4/

$(".task").die().live('click', function() {
    $(this).slideUp();
    return false;
});​

you will need to decide if you need the call to die depending on you scenario... I thorwed it in there anyways...

salek
  • 444
  • 4
  • 14