2

I am writing a code which adds rows dynamically into a cart. Their is delete button in front of every row to delete that row. When the user press button the parent of that button should be grabbed and then implement remove() method to remove that row. But this way its not working for me.. Can anybody tell me where i am doing wrong? Following is my code

      <div class="container" id="dynamic">
                 <div class="row " id="main-row">
                   <div class="col-sm-1  " id="serial">1</div>
                    <div class="col-sm-2  ">
                    <select class="container-fluid">
                        <option value="1">Computer</option>
                        <option value="2">Mobile</option>
                        <option value="3">LCD</option>
                        <option value="4">Keyboard</option>
                        <option value="5">Mouse</option>
                    </select>
                </div>
         <div class="col-sm-2 "><input class="length" type="number" min="1"          name="name" value="" /></div>
          <div class="col-sm-2 "><input class="length" type="text" name="text"      value="" min="1" /></div>
        <div class="col-sm-2"><input class="length" type="text" min="1"/></div>
                <button class="btn-primary col-sm-1 del ">Delete</button>
             </div>

          </div>
          <button class="btn btn-primary" id="button1">Add Row</button>
        </section> 

Here is my jquery code.

 //code for adding rows dynamically
 var counter = 1;
$(function () {

$("#button1").click(function () {
    counter++;
    $('#dynamic').append($("#main-row").clone().attr("id",counter-1));
    $("#serial").text(counter);

     });
 });
   //code for deleting row
  $('.del').click(function () {
    $(this).parent().remove();
   });
1
  • Please somebody answer me as i am stuck here at this point. I have tried couple of different things but nothing is working for me. Commented Mar 13, 2017 at 18:29

1 Answer 1

1

Since you're adding the rows dynamically, you'll need to use a delegated event handler.

Also use this instead of ".del" to avoid deleting all the rows.

$(document).on('click', '.del', function () {
  $(this).parent().remove();
 });

Otherwise, the new .del objects will not get the the click event.

Sign up to request clarification or add additional context in comments.

7 Comments

The button is not working. even if i changed .del to this.. There is no error on console as well
Nothing happen by pressing button. It don't delete the row
Does it delete the row in my Snippet? If so, we'll need to see more of your code.
Yes it is deleting in your snippet
please see the updated question. I have added the code of dynamically adding row. I think that might be one which is causing delete button not to work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.