0

I have dynamically added td content using jquery and added a functionality to remove the tr on click of td but the id is not working on dynamic content though its working on static I have dynamically added td content using jquery and added a functionality to remove the tr on click of td but the id is not working on dynamic content though its working on static I have dynamically added td content using jquery and added a functionality to remove the tr on click of td but the id is not working on dynamic content though its working on static I have dynamically added td content using jquery and added a functionality to remove the tr on click of td but the id is not working on dynamic content though its working on static

i have tried using jquery for the first time as i am not familiar to it

<div class="add-task">
                                        <div class="cross-icon add-new" id="hide-add-task">
                                            <img src="./images/Deleteicon.png" alt="">
                                        </div>
                                        <form class="add-task-form">
                                            <input type="text" id="task" class="add-task-input">
                                        </form>
                                    </div>
                                    <div class="custom-toggle">
                                        <div class="delete-task">
                                            <span>Are you sure you want to delete this task?</span>
                                            <div class="custom-choice">
                                                <span class="delete-row">Yes</span>
                                                <span id="hide-delete-alert">NO</span>
                                            </div>
                                        </div>
                                    </div>



<div class="custom-hyperlink"><span class="add-new-txt" id="show-add-task">Add New</span></div>



<tbody class="custom-table-body">
                                                <tr>
                                                    <td>
                                                        <label class="custom-checkbox">
                                                            <input type="checkbox" name="task">
                                                            <div>Check air pressure in tyres</div>
                                                            <span class="checkmark"></span>
                                                        </label>
                                                    </td>
                                                    <td><div class="ellipsis-txt">-</div></td>
                                                    <td><div class="ellipsis-txt">12/26/18</div></td>
                                                    <td><div class="ellipsis-txt">Service Center</div></td>
                                                    <td>
                                                        <img src="./images/Edit-icon.png" alt="edit" class="custom-img m-rt-16">
                                                        <img src="./images/delete-icon.png" alt="delete" class="custom-img" id="show-delete-alert">
                                                    </td>
                                                </tr>
                                            </tbody>


$(document).ready(function(){
            $("#hide-add-task").click(function(){
                $(".add-task").hide();
            });
            $("#show-add-task").click(function(){
                $(".add-task").show();
            });

            $("#hide-delete-alert").click(function(){
                $(".custom-toggle").hide();
            });
            $("#show-delete-alert").click(function(){
                $(".custom-toggle").show();
            });

            //ADD
            $(".add-new").click(function(){
            var task = $("#task").val();
            var markup = `<tr>
                            <td>
                                <label class='custom-checkbox'>
                                    <input type='checkbox' name='task'>
                                    <div>`+ task +`</div>
                                    <span class='checkmark'></span>
                                </label>
                            </td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td>
                                <img src='./images/Edit-icon.png' alt='edit' class='custom-img m-rt-16'>
                                <img src='./images/delete-icon.png' alt='delete' class='custom-img'>
                            </td>
                        </tr>`;

            $("table tbody").append(markup);
            });

            // DELETE
            $(".delete-row").click(function(){
                $("table tbody").find('input[name="task"]').each(function(){
                    if($(this).is(":checked")){
                        $(this).parents("tr").remove();
                    }
                });
            });
        });
juhi
  • 558
  • 1
  • 10
  • 19

3 Answers3

1

try :

$(document).on("click",".show-delete-alert",function(){ 
    // Do something
});
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Dhruv Raval
  • 1,535
  • 1
  • 8
  • 15
1

Try converting your markup variable to a jquery object before the append

var jqMarkup = $(markup);
$("table tbody").append(jqMarkup);
Alibech
  • 46
  • 3
0

You should use event delegation like below for existing one or future.

$(".delete-row").on('click',function() {
            $("table tbody").find('input[name="task"]').each(function(){
                if($(this).is(":checked")){
                    $(this).parents("tr").remove();
                }
            });
});