0

I have two functions in mycode.first is addElement() and second is removeElement() When I click on add it is adding elements dynamically, Now what I want to remove those elements dynamically when I click on remove. I tried .remove() and .empty() but its not working. Please help. add code:

$('#original').append('<br/><br/><strong>And &nbsp; </strong>');
                        var content = '<select name=selParameter' + count + ' id=selParameter' + count + ' class=chosen-select><option value=0>Select Parameter</option>';

remove code: $('#original').remove('#selParameter' + count + '');

7
  • Show the add and remove code. Commented Aug 27, 2015 at 11:30
  • Post your code fro add and remove functionality Commented Aug 27, 2015 at 11:30
  • 2
    possible duplicate of Remove element by id Commented Aug 27, 2015 at 11:32
  • Have you tried $('#selParameter' + count).remove()? Commented Aug 27, 2015 at 11:33
  • 1
    The 'add' code provided does not add the 'content' to the DOM. So there's nothing to remove. Please add the correct code or (as per other comment) create a jsfiddle where the add does add the elements correctly. Commented Aug 27, 2015 at 11:43

1 Answer 1

1

The problem here is you are adding the content to the HTML page after page is loaded, the same will work if you have done before the page load.

There is on function in jQuery to record changes and remove the elements dynamically when they are added. Please have a look at the following documentation http://api.jquery.com/on/

Let me know, if you still face issues.

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

2 Comments

which event am I suppose to use like load or else
$(document).ready(function(){ $("#add").on('click', function(){ alert('add'); $('body').append("<p id='added'> this is added </p>") }) $('#remove').on('click', function(){ alert('remove'); $('#added').remove() }) });

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.