-2

How do i change the content of an div on an website that has an id with javascript?

<div class="chat-area" id="chat-area">[Changeable]</div>

I want to know how i add content where it says "[Changeable]" with javascript so i can run the command through the console. I'd like if you keep your explainage simple, because im still very new to html/css/javascript ;)

Arunesh90
  • 33
  • 1
  • Are you aware that if you add/change the page content it will only be a temporary change unless you use a server-side langues to apply the new changes to the existing file? Javascript is client-side. – NewToJS Aug 13 '15 at 22:34
  • @NewToJS I'm going to use userscript/greasemonkey to solve that. – Arunesh90 Aug 13 '15 at 22:35
  • Okay, I just wanted to make sure you knew it wouldn't save the content as you change it. I have seen a few questions like this so thought I would make sure :) – NewToJS Aug 13 '15 at 22:36

2 Answers2

3

In very simple JavaScript terms, you can use:

document.getElementById("chat-area").innerHTML = 'New Content';

And yes, this would work in GreaseMonkey / UserScripts too!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    Ah thanks this was what i was looking for :) Edit: I'll mark this answered as soon those 10 minutes are over – Arunesh90 Aug 13 '15 at 22:37
2

You can use the innerHTML property to achieve your goal like so:

document.getElementById("chat-area").innerHTML = "Your new content here";
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Christine
  • 238
  • 1
  • 9