1

I have this code that when the user will click the Add button a Textbox will appear. But what I want to implement is to remove the textbox that was appended individually by clicking a button:

<div class="form_group">
   <div id="p_scents">
      <a class="btn btn-warning btn-sm" onclick="addAffiliate()">+ Add Merchant ID</a><br>
      <a class="btn btn-warning btn-sm" onclick="deleteAffiliate()">+ Remove Merchant ID</a><br>
      <p></p>
   </div>
</div>

My Javascript is:

function addAffiliate() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;
    $('<p><label for="p_scnts"><input type="text" required="true" id="p_scnt" size="30" class="form-control" name="merchant[]" value="" placeholder="Input MerchantID" /></label>  </p>').appendTo(scntDiv);
    i++;
}

My deleteAffiliate function is where I want to delete a single textbox that was appended before.

4
  • 1
    remove() with jquery and remove() with javascript Commented Nov 19, 2014 at 8:20
  • But what I want to add is to remove? o.O Commented Nov 19, 2014 at 8:21
  • It's little bit unclear. Commented Nov 19, 2014 at 8:24
  • im sorry for making it unclear..already edited Commented Nov 19, 2014 at 8:42

1 Answer 1

1

First of all you are appending several elements with same id "p_scnt" and you should avoid that. Change the function to:

function addAffiliate()
{
   var scntDiv = $('#p_scents');
   var i = $('#p_scents p').size() + 1;
   // here we change the id to p_scnt_ + iterator
    $('<p><label for="p_scnts"><input type="text" required="true" id="p_scnt_'+i+'" size="30" class="form-control" name="merchant[]" value="" placeholder="Input MerchantID" /></label>  </p>').appendTo(scntDiv);
            i++;

}

Then change the remove function to remove the last-child of parent.

function deleteAffiliate()
{
    $('#p_scents p:last-child').remove();
}
Sign up to request clarification or add additional context in comments.

Comments

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.