-3

I am trying to delete html elements dynamically.In the picture i have created a submit button whose id is va.When i select button from dropdown and put va in id and hit remove it should remove the element. Dynamic elements are getting created in registrationform div.

enter image description here

$("#remove").click(function(){
                var elementtocreate = $("#elementtocreate").val();
                var id = $("#id").val();
                console.log("Name : "+elementtocreate+"ID: "+id);

                $("#"+id).remove();


            });
Vallabh Lakade
  • 722
  • 7
  • 22
  • Try with live event for dynamic elements created after dom load: $("#remove").on('click', function() {...}); or $( "body" ).on( "click", "#remove", function() {...}); – Petroff Feb 06 '15 at 07:34
  • post your html please – Banana Feb 06 '15 at 07:34
  • Can be done, read about `jquery event delegation` http://learn.jquery.com/events/event-delegation/ and then, if you still have doubs, show us your code and what you tried so far. – gmo Feb 06 '15 at 07:35
  • where are the dynamic elements exactly, in the white div or in the parquet div? – Banana Feb 06 '15 at 07:36
  • everything in the div are dynamic elements.In the pic the submit button is dynamic – Vallabh Lakade Feb 06 '15 at 07:37
  • i am trying $("#"+id).remove(); but its not working – Vallabh Lakade Feb 06 '15 at 07:38
  • possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Banana Feb 06 '15 at 07:39

1 Answers1

0

Not sure, why it's not working for you, please check out this fiddle: http://jsfiddle.net/ozt4khmo/

(HTML)

<input type="text" name="elemId" id="elemId" placeholder="Element ID to be removed"/>
<button id="remove">Remove</button>

(JavaScript)

$('#remove').click(function() {
    var id = '#' + $('#elemId').val();
    $(id).remove();
});
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45