shaiss
shaiss

Reputation: 2010

Ajax replace instead of append

I used the following jQuery example which works like a charm. However it appends the results. What do I need to change to replace the results instead of appending?

Upvotes: 75

Views: 94535

Answers (4)

user8086037
user8086037

Reputation:

OK, last entry 2009, but if the problem still exist:

let oldElement = $('#results')
let content = '<div id="#results>9000+</div>'
oldElement = oldElement.replaceWith(content)
let newElement = $('#results')

Upvotes: 5

Namesh Silva
Namesh Silva

Reputation: 1

Empty function before your append or prepend function.

I tried this & it worked --->

frame is id selector & it append image.

$("#frame").empty().append('<img src="" >' ); 

Upvotes: 0

Marek Karbarz
Marek Karbarz

Reputation: 29304

you could empty the element before you append

$("#results").empty().append(myHtml);

or use the html method

$("#results").html(myHtml)

Upvotes: 157

Greg
Greg

Reputation: 321638

Just change

$('#results').append(myHtml);

to

$('#results').html(myHtml);

Upvotes: 27

Related Questions