Danusorn Danusorn - 2 years ago 262
Javascript Question

Jquery remove br tag on above

i create dynamic field that can add and can remove.

but in my script remove button will remove input field and br all line

i want it to only that i click

var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="meaning[]" class="form-control" /><a href="#" class="remove_field"> <span class="glyphicon glyphicon-trash" aria-hidden="true" id="trash"></span></a></div>'); //add input box



$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
$(this).parent('div').remove(); x--;

this code after i click remove it remove all br

how can i do if i want remove only parent div

Answer Source

Try using prev() function:

//Strore $(this) to a variable in order not to call jQuery multiple times
var that = $(this);
that.parent('div').remove(); x--;

Check the documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download