0

I have specific div that I want to copy with jQuery. After copying, I want to modify it a little bit and the remove an Input with the ID CustomerDeliveryaddressID. Finally I want to append it somewhere. Unfortunately, the deletion of the part is not working for me. How can I do that? Here is my Code.

  $("#addDeliveryAddress").click(function(){
    var content = $('.deliveryAddressBox').html();
    content = "<div class='deliveryAddressBox'>"+content+"</div>";
    content = $(content).remove("#CustomerDeliveryaddressId");
    $(".deliveryAddressesContainer").append(content);
  });
1
  • Double check that remove() is not returning the part that is removed. If it is, you are setting the content with what was removed. Commented May 8, 2019 at 14:09

3 Answers 3

1

Instead of removing and saving into content try replacing your remove line with

$("#addDeliveryAddress").click(function(){
    var $content = $('.deliveryAddressBox:last').clone();
    $content.wrapAll( "<div class='deliveryAddressBox'/>");
    $content.find("#CustomerDeliveryaddressId").remove();
    $(".deliveryAddressesContainer").append($content);
});
Sign up to request clarification or add additional context in comments.

4 Comments

I replaced your code with the line. But it's still not working. I checkout out with only the "find", if it finds the right supposed input. It is finding it. But removing it does somehow not work.
Check now, I took clone instead of html
That's looking good. There is just one thing for the finetuning. I have several divs with .deliveraAddressBox. How is it possible to just take the last one with clone? Otherwise it duplicates all (so if I have two divs with this class, it creates 4 divs instead of just one).
See I updated my answer, you need to append :last to element which wants you to fetch in the index of DOM
0

Make sure to not store the response from remove() in a variable that overwrites the value you want.

$("#addDeliveryAddress").click(function(){
    var $content = $( $('.deliveryAddressBox').html() );

    // wrap the contents with the div
    $content.wrapAll("<div class='deliveryAddressBox'>");
    // remove the element you do not want
    $content.find("#CustomerDeliveryaddressId").remove();
    // append it to the DOM
    $(".deliveryAddressesContainer").append($content);
});

Comments

0

$("#addDeliveryAddress").click(function(){
    var content = $('.deliveryAddressBox').html();
    content = "<div class='deliveryAddressBox'>"+content+"</div>";
    content = $(content).remove("#CustomerDeliveryaddressId");
    $(".deliveryAddressesContainer").append(content);
  });
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Hi , Replace $(content).remove("#CustomerDeliveryaddressId"); with $(content).remove("#CustomerDeliveryaddressId");

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.