/    Sign up×
Community /Pin to ProfileBookmark

Adding text to a <div>

I’m having some trouble adding text to a div. More specifically, I can’t seem to be able to add text to the text I’ve added before, it just replaces the old text every time.

I’ve tried

[CODE]function update(newMessage){
var next = document.createElement(“p”);
next.innerHTML = newMessage;
document.getElementById(‘textbox’).appendChild(next);
}[/CODE]

and

[CODE]function update(newMessage){
document.getElementById(‘textbox’).innerHTML += newMessage;
}[/CODE]

Can anyone tell me what I’m doing wrong? Because as far as I can see, even though I’m seeing the text, nothing seems to be added to the html itself when I look at the source.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rpg2009Jan 08.2011 — If you're using firefox try installing the webdeveloper plug-in. https://addons.mozilla.org/en-US/firefox/addon/60/

Then click on view source -> view generated source

What you are seeing is the original source before it's been ammended.
Copy linkTweet thisAlerts:
@JMRKERJan 08.2011 — What does the rest of your code look like? Anything like this?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Info Append&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=240712

function update(newMessage){
document.getElementById('textbox').innerHTML += newMessage;
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Append Info&lt;/h1&gt;
&lt;input type="text" id="TBox" value="" size="40"&gt;
&lt;input type="button" onclick="update(document.getElementById('TBox').value)" value="New info"&gt;&lt;p&gt;

&lt;div id="textbox"&gt;
Original information in this section.&lt;p&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@rpg2009Jan 08.2011 — What does the rest of your code look like? Anything like this?[/QUOTE]

Are you referring to the page loading i.e. onLoad or placing the script at the end of the body?

If you read his comment

Can anyone tell me what I'm doing wrong? Because as far as I can see, even though [B]I'm seeing the text[/B][/QUOTE]

It appears to be working, it's just the source code he's viewing doesn't reflect that.

If you're not, forget about it?
Copy linkTweet thisAlerts:
@JMRKERJan 08.2011 — Are you referring to the page loading i.e. onLoad or placing the script at the end of the body?

If you read his comment



It appears to be working, it's just the source code he's viewing doesn't reflect that.

If you're not, forget about it?[/QUOTE]


OP does not have enough code to tell what the problem is at this time.

My attempt was to demonstrate the function will work if the supporting code looks similar to the posted script.
Copy linkTweet thisAlerts:
@rpg2009Jan 08.2011 — Apologies. My mistake.
×

Success!

Help @Gorgro spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...