topper1309
topper1309

Reputation: 151

Adding and removing div in jquery not working

I am trying to add question box as shown below, the first remove button works fine. But when I dynamically add the div and then try to remove it does not work. I tried to alert on deleting question it still does not work. Can anyone help me on this ?

JS

<script type="text/javascript">
jQuery(document).ready(function(e) {

    jQuery( '#add-question' ).on('click', function() {
        var lastId = jQuery('.del-question').last().data('id');
        var nextId = lastId+1;
        var questionBox = '<div class="question-box box-1"><h2>Question 1</h2><input type="text" name="ques_title" class="ques_title" placeholder="Enter question title" value=""><div class="form-group"><a class="del-question button" href="#" data-id="1">Remove</a></div></div>';
        jQuery('.question-box').after(questionBox);
        return false;
    });

    jQuery( '#quiz-box .del-question' ).on('click', function() {
        var id = jQuery(this).data('id');
        jQuery('.box-'+id).remove();
        return false;
    });
});
</script>

HTML

<div id="quiz-box">
  <div class="question-box box-1">
   <h2>Question 1</h2>
   <input type="text" name="ques_title" class="ques_title" placeholder="Enter question title" value="">
   <div class="form-group">
    <a class="del-question button" href="#" data-id="1">Remove</a>
   </div>
 </div>
 <div class="form-group">
  <a id="add-question" class="button" href="#">Add</a>
 </div>

Upvotes: 0

Views: 727

Answers (1)

SomeRSGuy
SomeRSGuy

Reputation: 600

I made a Snippet for you.

The problem was that you can't bind events to dynamic objects. $(document).on is needed.

Plus, I used clone() and added a "renameQuestions" function, just to give you a better way to approach what you want.

$(document).ready(function(e) {
    function addQuestion(){
        var question = $('#question-template').clone();
        question.css("display","block").removeAttr('id');
        $('#questions').append(question);
    }

    function renameQuestions(){
        $('.question-box').each(function(i,v){
            $(this).find('.question_id').html(i);
        });
    }

    $('#add-question').on('click', function() {
        addQuestion();
        renameQuestions();
    });

    $(document).on('click','.del-question', function()
    {
        $(this).closest('.question-box').remove();
        renameQuestions();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="questions">
  <div class="question-box" id="question-template" style="display:none;">
    <h2>Question <span class="question_id"></span></h2>
    <input type="text" name="ques_title" class="ques_title" placeholder="Enter question title" value="">
    <div class="form-group">
      <a class="del-question button" href="#">Remove</a>
    </div>
  </div>
</div>

<div class="form-group" style="margin-top:20px;">
  <a id="add-question" class="button" href="#">Add</a>
</div>

Upvotes: 1

Related Questions