Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery .on() isn't binding click events to dynamically created elements [duplicate]

I have a textbox that dynamically adds an element when I press enter, and another that deletes the element when I click the delete button. The delete method works for any existing elements, but doesn't work for any elements that were dynamically inserted.

Here's the code:

$ ->
    # AJAX to add a new stock
    $("#add-symbol").keypress (e) ->
        if e.which == 13
            url = $(this).data('url')
            name = $(this).val()
            $.ajax
                url: url
                type: "POST"
                data: {
                    user_id: $('#info').data('user-id'),
                    name: name
                }
                success: (response) ->
                    if response.status == 200
                        new_element = '<a class="item" data-path="' + response.path + '" data-stock="' + response.symbol + '">'+ response.symbol + '<i class="icon remove"></i></a>'
                        $('#symbols').append(new_element)
                        $('#add-symbol').val('')
                    else
                     #deal with errors

    # AJAX to delete stocks
    $('.icon.remove').on('click', (e) ->
        console.log('click click')
        $parent = $($(this).parent().get(0))
        stock = $parent.data('stock')
        user_id = $("#info").data('user-id')
        url = $parent.data('path')

        $.ajax
            url: url
            type: "DELETE"
            data: {
                user_id: user_id,
                name: stock
            }
            success: (response) ->
                if response.status == 200
                    $parent.remove()
                else
                    # deal with errors
    )

Any ideas? From what I've read, .on() should fix the issue of binding a click event to a dynamically generated element, but it doesn't seem to be working.

like image 616
chintanparikh Avatar asked Oct 14 '13 23:10

chintanparikh


People also ask

How to bind click event to dynamically created elements in jQuery?

on() method. In the example, I have created an unordered list and a button for adding new list items. In the script, attach click event on #lists li selector to change the background color of clicked <li> . Append a new <li> to the <ul> element on the add button click and also attaching click event on <li> using .

How to bind click event in JavaScript for dynamically added HTML element?

Attaching the event dynamicallyclassName = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!

How can we call onclick function on dynamically created button in jQuery?

onclick = function () { alert("hi jaavscript"); };


1 Answers

this is wrong: $('.icon.remove').on('click'...

this is right: $(document).on('click', '.icon.remove', function)

you can use any container instead of document (which is the most highlevel container).

hope that helps

like image 106
geevee Avatar answered Sep 21 '22 05:09

geevee