tags:

views:

37

answers:

4

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

<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 reply...

+4  A: 

Just wrap all those elements in a div and remove them

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

or do it a little more friendlier

$('#select-wrapper').fadeOut(500, function() { $(this).remove(); });
alex
Thanks for the answer...I knew it was easy.....:(
Jerry
+1  A: 

try putting the content you want removed in its own div. Then it will be simple jquery to hide/remove the div on the click event.

rzzmttzz
Thanks for the tip. +1
Jerry
+1  A: 

You could do this:

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

This just removes all children using .remove() without the close class on them. Or just clone and re-add the link using .clone(), like this:

$('.model').click(function(e) {  
  $("#dialog a.close").clone(true).appendTo($("#dialog").empty());
});
Nick Craver
True, but that won't remove text nodes (nodeType == 3's). I think the way to go here would be to incorporate a wrapper as per @alex's suggestion.
karim79
@karim - I'd actually clone the `<a>`, re-add it, but I'm not sure of his exact markup...hard to tell what's nested from the question
Nick Craver
Thanks. I love your solution...:D +1.
Jerry
+1  A: 

Do you really want to keep that a link, or do you just want that div to contain a link that looks just like it afterwards?

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

I understand that if that link has changed in some way beyond your knowledge or control, then you'd have to keep it, but unless that is the case, a lookalike will do the trick.

Paul Butcher
I need to keep that link to make my jquery works. Thanks for the tip though. +1
Jerry