I have many in my html and want to add more through javascript. However, using innerHTML is just replacing my existing sections with new ones, instead of adding them to the old ones.
Đang xem: Can't get jquery (or javascript) pass my variable to the next page
What else can I use?
kenorb
144k76 gold badges655 silver badges710 bronze badges
asked Nov 21, 2012 at 14:05
1
You can use
document.getElementById(“parentID”).appendChild(/*..your content created using DOM methods..*/)or
document.getElementById(“parentID”).innerHTML+= “new content”
Amberlamps
37k5 gold badges39 silver badges53 bronze badges
answered Nov 21, 2012 at14:07
Sajjan SarkarSajjan Sarkar
3,6924 gold badges38 silver badges48 bronze badges
3
I Just came across to a similar to this question solution with included some performance statistics.
It seems that example below is faster:
document.getElementById(“container”).insertAdjacentHTML(“beforeend”, “
Xem thêm: Nắm Bắt Tâm Lý Đàn Ông Khi Người Đàn Ông Im Lặng, Đàn Ông Nghĩ Gì Khi Chia Tay Trong Im Lặng
Reference: 1) Performance stats 2) API – insertAdjacentHTML
I hope this will help.
answered Jul 22, 2018 at 19:33
Xem thêm: Điều Trị Ung Thư Thận Rcc – Ung Thư Thận: Những Điều Cần Biết
speksyspeksy
6208 silver badges12 bronze badges
2
I think if you want to add content directly to the body, the best way is:
document.body.innerHTML += “bla bla”;To replace it, use:
document.body.innerHTML = “bla bla”;
answered Jan 16, 2015 at 10:30
WebbingWebbing
2632 silver badges2 bronze badges
3
Try the following syntax:
document.body.innerHTML += “My new content
“;
answered Aug 3, 2017 at 11:28
kenorbkenorb
144k76 gold badges655 silver badges710 bronze badges
4
You”re probably using
document.getElementById(“element”).innerHTML = “New content”Try this instead:
document.getElementById(“element”).innerHTML += “New content”Or, preferably, useDOM Manipulation:
document.getElementById(“element”).appendChild(document.createElement(“div”))Dom manipulation would be preferred compared to using innerHTML, because innerHTML simply dumps a string into the document. The browser will have to reparse the entire document to get it”s stucture.