Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: How to clear an element before appending new content?

Tags:

jquery-ui

This is my jquery code

 $.ajax({    url: "PopUpProductDetails.aspx",             cache: false      }).done(function (html) {      $("#dialog").append(html);  }); 

The first time, it works just fine. It display the content of the PopUpProductDetails.aspx page. But, after that, If I click again, I get twice the same content, and so forth. I believe the problem is that I need to clear the dialog element first, before appending new content.

How do I do that?

like image 447
Richard77 Avatar asked Dec 11 '12 14:12

Richard77


People also ask

How do I remove appended data?

Approach 1: Initially use removeClass() method to remove active class of previously appended data menu item. Then followed by addClass() method to add active class of currently appended data menu item. Now use each() function to append data with respect active class added or removed on click.

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

What does .append do in jQuery?

The . append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use . prepend() ).


2 Answers

.append() appends html to the end of existing html string, use .html() to replace what's currently inside of #dialog.

like image 124
package Avatar answered Sep 23 '22 01:09

package


Inside the function clear the dialog first and then fill up with the content

$.ajax({    url: "PopUpProductDetails.aspx",             cache: false      }).done(function (html) {      $("#dialog").html("");      $("#dialog").html(html);  }); 
like image 43
Rashedul.Rubel Avatar answered Sep 22 '22 01:09

Rashedul.Rubel