(jQuery) Adding text wipes out everything else in the div

Multi tool use
I'm working on the comments part of my site, and when I dynamically create the anchor
tag, but it doesn't show up. Probably because it gets erased by the .text()
function called right after appending it to the div
.
Here's my function to create the div
:
function createCommentDiv(userName, content, date)
{
avatarImage = $("<img/>")
.attr('src', "{{ path('view_photo', {id: photo.id}) }}")
.attr('width', 42)
.addClass("avatar");
avatarDiv = $("<div/>")
.addClass("avatar")
.append(avatarImage);
userLink = $("<a/>") // this anchor
.attr("href", "../profile.html") // does not show up
.addClass("user") // in
.text(userName); // the DOM
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content); // I think this cancels out the userLink anchor
dateDiv = $("<div/>")
.addClass('date')
.text(date);
contentDiv = $("<div/>")
.addClass("content")
.append(userWrapperDiv)
.append($("<div/>").addClass('clear'))
.append(dateDiv);
commentDiv = $("<div/>")
.addClass("comment")
.attr('style', 'background-color: lightgreen')
.append(avatarDiv)
.append(contentDiv)
.append($("<div/>").addClass('clear'));
return commentDiv;
}
This is what it is supposed to be produced:
<div class="comment" style="background-color: lightgreen ">
<div class="avatar">
<img src="/photo/34/view" class="avatar" width="42">
</div>
<div class="content">
<div class="wrapper">
<a href="../profile.html" class="user">Bob Smith</a> // This link disappears from the DOM
Reply 2
</div>
<div class="clear"></div>
<div class="date">
December 28 at 03:34 AM
</div>
</div>
<div class="clear"></div>
</div>
I think it's because calling the .text(content)
erases the anchor
html that is previously added to the userWrapperDiv
. But I don't know the proper way to do this then.
Here is a picture of what happens:
How do you add text to the end of a div without erasing the rest of it's contents?
jquery html text
add a comment |
I'm working on the comments part of my site, and when I dynamically create the anchor
tag, but it doesn't show up. Probably because it gets erased by the .text()
function called right after appending it to the div
.
Here's my function to create the div
:
function createCommentDiv(userName, content, date)
{
avatarImage = $("<img/>")
.attr('src', "{{ path('view_photo', {id: photo.id}) }}")
.attr('width', 42)
.addClass("avatar");
avatarDiv = $("<div/>")
.addClass("avatar")
.append(avatarImage);
userLink = $("<a/>") // this anchor
.attr("href", "../profile.html") // does not show up
.addClass("user") // in
.text(userName); // the DOM
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content); // I think this cancels out the userLink anchor
dateDiv = $("<div/>")
.addClass('date')
.text(date);
contentDiv = $("<div/>")
.addClass("content")
.append(userWrapperDiv)
.append($("<div/>").addClass('clear'))
.append(dateDiv);
commentDiv = $("<div/>")
.addClass("comment")
.attr('style', 'background-color: lightgreen')
.append(avatarDiv)
.append(contentDiv)
.append($("<div/>").addClass('clear'));
return commentDiv;
}
This is what it is supposed to be produced:
<div class="comment" style="background-color: lightgreen ">
<div class="avatar">
<img src="/photo/34/view" class="avatar" width="42">
</div>
<div class="content">
<div class="wrapper">
<a href="../profile.html" class="user">Bob Smith</a> // This link disappears from the DOM
Reply 2
</div>
<div class="clear"></div>
<div class="date">
December 28 at 03:34 AM
</div>
</div>
<div class="clear"></div>
</div>
I think it's because calling the .text(content)
erases the anchor
html that is previously added to the userWrapperDiv
. But I don't know the proper way to do this then.
Here is a picture of what happens:
How do you add text to the end of a div without erasing the rest of it's contents?
jquery html text
1
Can you just.append(content)
rather than.text(content)
? (.text
will remove whatever existed in the container before)
– CertainPerformance
Dec 30 '18 at 9:01
Hi CertainPerformance, that was it.
– Brent Heigold
Dec 30 '18 at 14:27
add a comment |
I'm working on the comments part of my site, and when I dynamically create the anchor
tag, but it doesn't show up. Probably because it gets erased by the .text()
function called right after appending it to the div
.
Here's my function to create the div
:
function createCommentDiv(userName, content, date)
{
avatarImage = $("<img/>")
.attr('src', "{{ path('view_photo', {id: photo.id}) }}")
.attr('width', 42)
.addClass("avatar");
avatarDiv = $("<div/>")
.addClass("avatar")
.append(avatarImage);
userLink = $("<a/>") // this anchor
.attr("href", "../profile.html") // does not show up
.addClass("user") // in
.text(userName); // the DOM
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content); // I think this cancels out the userLink anchor
dateDiv = $("<div/>")
.addClass('date')
.text(date);
contentDiv = $("<div/>")
.addClass("content")
.append(userWrapperDiv)
.append($("<div/>").addClass('clear'))
.append(dateDiv);
commentDiv = $("<div/>")
.addClass("comment")
.attr('style', 'background-color: lightgreen')
.append(avatarDiv)
.append(contentDiv)
.append($("<div/>").addClass('clear'));
return commentDiv;
}
This is what it is supposed to be produced:
<div class="comment" style="background-color: lightgreen ">
<div class="avatar">
<img src="/photo/34/view" class="avatar" width="42">
</div>
<div class="content">
<div class="wrapper">
<a href="../profile.html" class="user">Bob Smith</a> // This link disappears from the DOM
Reply 2
</div>
<div class="clear"></div>
<div class="date">
December 28 at 03:34 AM
</div>
</div>
<div class="clear"></div>
</div>
I think it's because calling the .text(content)
erases the anchor
html that is previously added to the userWrapperDiv
. But I don't know the proper way to do this then.
Here is a picture of what happens:
How do you add text to the end of a div without erasing the rest of it's contents?
jquery html text
I'm working on the comments part of my site, and when I dynamically create the anchor
tag, but it doesn't show up. Probably because it gets erased by the .text()
function called right after appending it to the div
.
Here's my function to create the div
:
function createCommentDiv(userName, content, date)
{
avatarImage = $("<img/>")
.attr('src', "{{ path('view_photo', {id: photo.id}) }}")
.attr('width', 42)
.addClass("avatar");
avatarDiv = $("<div/>")
.addClass("avatar")
.append(avatarImage);
userLink = $("<a/>") // this anchor
.attr("href", "../profile.html") // does not show up
.addClass("user") // in
.text(userName); // the DOM
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content); // I think this cancels out the userLink anchor
dateDiv = $("<div/>")
.addClass('date')
.text(date);
contentDiv = $("<div/>")
.addClass("content")
.append(userWrapperDiv)
.append($("<div/>").addClass('clear'))
.append(dateDiv);
commentDiv = $("<div/>")
.addClass("comment")
.attr('style', 'background-color: lightgreen')
.append(avatarDiv)
.append(contentDiv)
.append($("<div/>").addClass('clear'));
return commentDiv;
}
This is what it is supposed to be produced:
<div class="comment" style="background-color: lightgreen ">
<div class="avatar">
<img src="/photo/34/view" class="avatar" width="42">
</div>
<div class="content">
<div class="wrapper">
<a href="../profile.html" class="user">Bob Smith</a> // This link disappears from the DOM
Reply 2
</div>
<div class="clear"></div>
<div class="date">
December 28 at 03:34 AM
</div>
</div>
<div class="clear"></div>
</div>
I think it's because calling the .text(content)
erases the anchor
html that is previously added to the userWrapperDiv
. But I don't know the proper way to do this then.
Here is a picture of what happens:
How do you add text to the end of a div without erasing the rest of it's contents?
jquery html text
jquery html text
edited Dec 30 '18 at 11:41


Rishabh Agarwal
912320
912320
asked Dec 30 '18 at 8:59
Brent HeigoldBrent Heigold
129110
129110
1
Can you just.append(content)
rather than.text(content)
? (.text
will remove whatever existed in the container before)
– CertainPerformance
Dec 30 '18 at 9:01
Hi CertainPerformance, that was it.
– Brent Heigold
Dec 30 '18 at 14:27
add a comment |
1
Can you just.append(content)
rather than.text(content)
? (.text
will remove whatever existed in the container before)
– CertainPerformance
Dec 30 '18 at 9:01
Hi CertainPerformance, that was it.
– Brent Heigold
Dec 30 '18 at 14:27
1
1
Can you just
.append(content)
rather than .text(content)
? (.text
will remove whatever existed in the container before)– CertainPerformance
Dec 30 '18 at 9:01
Can you just
.append(content)
rather than .text(content)
? (.text
will remove whatever existed in the container before)– CertainPerformance
Dec 30 '18 at 9:01
Hi CertainPerformance, that was it.
– Brent Heigold
Dec 30 '18 at 14:27
Hi CertainPerformance, that was it.
– Brent Heigold
Dec 30 '18 at 14:27
add a comment |
2 Answers
2
active
oldest
votes
You can try with .append() instead of .text()
add a comment |
.text()
will indeed replace the whole content of the element. You can append a text node instead, using document.createTextNode
:
Replace:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content);
with:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.append(document.createTextNode(content));
You can also use .append(content)
directly, but note that this will add any HTML as is, just like .html()
would (as opposed to .text()
).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976356%2fjquery-adding-text-wipes-out-everything-else-in-the-div%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try with .append() instead of .text()
add a comment |
You can try with .append() instead of .text()
add a comment |
You can try with .append() instead of .text()
You can try with .append() instead of .text()
answered Dec 30 '18 at 9:04


MaheskumarMaheskumar
1215
1215
add a comment |
add a comment |
.text()
will indeed replace the whole content of the element. You can append a text node instead, using document.createTextNode
:
Replace:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content);
with:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.append(document.createTextNode(content));
You can also use .append(content)
directly, but note that this will add any HTML as is, just like .html()
would (as opposed to .text()
).
add a comment |
.text()
will indeed replace the whole content of the element. You can append a text node instead, using document.createTextNode
:
Replace:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content);
with:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.append(document.createTextNode(content));
You can also use .append(content)
directly, but note that this will add any HTML as is, just like .html()
would (as opposed to .text()
).
add a comment |
.text()
will indeed replace the whole content of the element. You can append a text node instead, using document.createTextNode
:
Replace:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content);
with:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.append(document.createTextNode(content));
You can also use .append(content)
directly, but note that this will add any HTML as is, just like .html()
would (as opposed to .text()
).
.text()
will indeed replace the whole content of the element. You can append a text node instead, using document.createTextNode
:
Replace:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.text(content);
with:
userWrapperDiv = $("<div/>")
.addClass('wrapper')
.append(userLink)
.append(document.createTextNode(content));
You can also use .append(content)
directly, but note that this will add any HTML as is, just like .html()
would (as opposed to .text()
).
edited Dec 30 '18 at 9:09
answered Dec 30 '18 at 9:04


JetoJeto
5,22521018
5,22521018
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976356%2fjquery-adding-text-wipes-out-everything-else-in-the-div%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
64cR7JdOk9f9 H ilI3I NQUOyYaN b,hX6SnmZaYMwBbgV8eRaxdF1joqbUsYSlVkLb L,7VifFATR4TDBwqr,F,rTl Hm0M9M
1
Can you just
.append(content)
rather than.text(content)
? (.text
will remove whatever existed in the container before)– CertainPerformance
Dec 30 '18 at 9:01
Hi CertainPerformance, that was it.
– Brent Heigold
Dec 30 '18 at 14:27