วันจันทร์ที่ 17 พฤษภาคม พ.ศ. 2553

JQuery append and bind - Stack Overflow

2
1


I have an unordered list with elements and I want to append an item at the end. Here is the current code:

The initial list:



  • Some text


  • Some text



The code that removes a list item:

$(".remove").click(function() {
$(this).parent().remove();
});

The code that adds a new list item:

$("#add").click(function() {
$("#all").append(
"
  • "
    + "Some text"
    + ""
    + "
  • "
    );
    });

    The button to add a new list item:



    When I click the button, a new list indeed is added to the list, but clicking the remove button doesn't do anything.

    How do I make this work?

    Bonus: Change "Some text" with " " and you will see that the two newly added input elements will have different distance between them from the initial ones. Why? (NOTE: using Firefox 3.0.5).
    jquery
    flag

    asked Jan 27 '09 at 22:07
    pek
    1,8861033

    84% accept rate


    2 Answers
    oldest newest votes
    7


    I think what you're after is one of the new jQuery 1.3 features - live events. See http://docs.jquery.com/Events/live.

    This works for me:

    $(".remove").live("click", function() {
    $(this).parent().remove();
    });

    Bonus:

    I'm also using FF 3.0.5, and I have the same amount of space between the two textboxes. If you mean between the second textbox and the button, then I'd have to agree with eimaj and say whitespace is the cause.
    link|flag

    edited Jan 27 '09 at 22:27

    ไม่มีความคิดเห็น: