0

I have this HTML

<ul class="list-row">
    <li>
        <input name="field1" class="field1" type="text" placeholder="field 1">
        <input name="field2" class="field2" type="text" placeholder="field 2">
        <p class="delrow">Delete row</p> 
    </li>
    <li>
        <input name="field1" class="field1" type="text" placeholder="field 1">
        <input name="field2" class="field2" type="text" placeholder="field 2">
        <p class="delrow">Delete row</p> 
    </li>
    <p class="addrow">Add row</p> 
</ul>

And when i click on Add row i append a new li with the elements that i need. The function on jQuery is this one:

$(".addrow").on("click",function() {
    $(this).before("<li>\n\
        <input name='field1' class='field1' type='text' placeholder='field 1' />\n\
        <input name='field2' class='field2' type='text' placeholder='field 2' />\n\
        <p class='delrow'>Delete row</p></li>");

});

What i need is to attach the click event to the new delrow that is appended to the DOM. I tried selecting getting the siblings and finding the p element but i can't make it work.

abaracedo
  • 1,090
  • 3
  • 21
  • 41

5 Answers5

2

This code should work:

$('.list-row').on('click','.delrow',function(){/*do stuff*/});

It will add an event handler to list row, so it will work for appended delrows

orhanhenrik
  • 1,397
  • 7
  • 11
0

You can use insertBefore method for this:

$(".addrow").on("click",function() {
    var newElement = $("<li>\n\
        <input name='field1' class='field1' type='text' placeholder='field 1' />\n\
        <input name='field2' class='field2' type='text' placeholder='field 2' />\n\
        <p class='delrow'>Delete row</p></li>").insertBefore( this );

    newElement.find( '.delrow' ).click( function() {
        //your code
    });

});

In this case newElement variable will point to dynamically created HTML

antyrat
  • 26,266
  • 9
  • 69
  • 74
0
$("body").on('click', '.delrow', function () {
   ...
});

Add the click event handler to the body, any click event will bubble up the DOM until it is handled. Here we're looking for a click at the body level, but with an originating element of class delrow. As the body always exists, it will handle clicks for all new elements of .delrow automatically.

michaelward82
  • 4,064
  • 21
  • 35
0

Add a class to the

  • element. Then you can use $(".liclass").on("click",function(){do something}); No matter how many
  • elements you add, you can then have the same handler.
  • Ryan
    • 77
    • 4
    0

    Just bind it right after you append:

    $(".addrow").on("click",function() {
        $(this).before("<li>\n\
            <input name='field1' class='field1' type='text' placeholder='field 1' />\n\
            <input name='field2' class='field2' type='text' placeholder='field 2' />\n\
            <p class='delrow'>Delete row</p></li>");
        });
    
        $(this).prev("li").find(".delrow").click(function() {});
    });
    
    tymeJV
    • 99,730
    • 13
    • 150
    • 152