admin管理员组文章数量:1400097
I am using below code to append some data which getting from api
for (i = 0; i < d.length; i++) {
//alert(d[i].food_name);
$('#myModal .modal-body').append("<span>" +
d[i].food_name + "</span><span>" +
d[i].redeem_code + "</span><span
class='food_price'>" + d[i].food_price + "
</span><span class='food-check'><input value=" +
d[i].food_id + " type='radio' redeem=" +
d[i].redeem_code + " food-price=" +
d[i].food_price + " food_name="+ d[i].food_name
+ " name='food' class='select'></span><br>");
}
When the api is hitting second time the old data is also showing with new data. To solve this I tried with .html()
but using this only the last record is showing. Please help. Not getting it.
I am using below code to append some data which getting from api
for (i = 0; i < d.length; i++) {
//alert(d[i].food_name);
$('#myModal .modal-body').append("<span>" +
d[i].food_name + "</span><span>" +
d[i].redeem_code + "</span><span
class='food_price'>" + d[i].food_price + "
</span><span class='food-check'><input value=" +
d[i].food_id + " type='radio' redeem=" +
d[i].redeem_code + " food-price=" +
d[i].food_price + " food_name="+ d[i].food_name
+ " name='food' class='select'></span><br>");
}
When the api is hitting second time the old data is also showing with new data. To solve this I tried with .html()
but using this only the last record is showing. Please help. Not getting it.
- remove the append() and use html(). If you have more elements then give the $('#myModal .modal-body').html("") before starting the for loop – Karthi Keyan Commented Jun 4, 2018 at 10:10
- by html() the only last record is showing in modal – Sagar Kodte Commented Jun 4, 2018 at 10:11
- i edited my ment check it – Karthi Keyan Commented Jun 4, 2018 at 10:12
- @KarthiKeyan. I'm getting 6 records from the api when i use .html(). Only the last record from the api is showing. – Sagar Kodte Commented Jun 4, 2018 at 10:14
- either give .html("") before the loop or give .html("") if (i==0) – Karthi Keyan Commented Jun 4, 2018 at 10:21
4 Answers
Reset to default 3You need to clear the html before you go into the loop. So keep the append, but use the below before the loop:
$('#myModal .modal-body').empty();
See this JSfiddle as an example.
use .empty() to empty content in a element
$('#myModal .modal-body').empty().append()
You can use replaceWith()
method, it replaces selected elements with new content.
You need to store the html in a variable while iterating through and can replace the element.
let html = '';
for (i = 0; i < d.length; i++) {
html += `<span> ${d[i].food_name} </span>
<span> ${d[i].redeem_code}</span>
<span class='food_price'> ${d[i].food_price} </span>
<span class='food-check'>
<input value=" ${d[i].food_id} type='radio' redeem='${d[i].redeem_code}' food-price= ${d[i].food_price} food_name="${d[i].food_name} name='food' class='select'>
</span>
<br>`
}
$('#myModal .modal-body').replaceWith(html);
Use .html("")
instead of .html()
to clear innerhtml of your selected DOM.
And about your question.
Var newHtml=" ";
For(loop)
{
newHtml= newHtml+ "your data[i]";
}
OldHtml.html(" ");
OldHtml.html(newHtml);
The above code will append new data to your html.
本文标签: javascriptHow to remove old html when appending new html contentStack Overflow
版权声明:本文标题:javascript - How to remove old html when appending new html content? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744239083a2596698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论