Remove items and content before item

I am trying to remove elements and content before a link inside a div when the user clicks a button. What is the best way to do this?

<div id="dialog" class="window">  

         //will be inserted a <select> element and few text here 
         //but I want to clear them after the user click a button

    <a href="#" class="close">Close it</a>  // I want to keep this <a> link.    

    </div>  

My jquery

$('.model').click(function(e) {  

   $("#dialog").empty();  //I can't use this because <a> will be deleted. Any better ideas?         

});

Thanks for the answer...

+3
source share
4 answers

Just wrap all these elements in a div and delete them

$('#select-wrapper').remove();

or make it a little more friendly

$('#select-wrapper').fadeOut(500, function() { $(this).remove(); });
+5
source

try putting the content you want to remove in your own div. Then there will be just jquery to hide / remove the div in the click event.

+1
source

:

$('.model').click(function(e) {  
  $("#dialog").children(":not(.close)").remove();
});

, .remove() close . , .clone(), :

$('.model').click(function(e) {  
  $("#dialog a.close").clone(true).appendTo($("#dialog").empty());
});
+1

a, , div , , ?

Why not just rewrite innerHTML with <a href="#" class="close">Close it</a>?

I understand that if this link has somehow changed beyond your knowledge or control, then you will have to save it, but if it is not, then the one looking will do the trick.

+1
source

Source: https://habr.com/ru/post/1747910/


All Articles