48

I'm making a mini-game where a player attacks a npc and in the center is a white box (div) that I call (log), because when the player damages/attacks the npc, I want to be able for that open white space to log what happened.

I'm using the getElementById(log) and then adding something along the lines of "document.write("You attacked X npc"), but its not working.

Any idea on how I can get text INSIDE the box and not outside it? Thanks

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Shawn
  • 1,175
  • 2
  • 11
  • 20

5 Answers5

78

document.getElementById('log').innerHTML += '<br>Some new content!';
<div id="log">initial content</div>
plalx
  • 42,889
  • 6
  • 74
  • 90
43

You can use one of the following methods:

document.getElementById('log').innerHTML = "text";

document.getElementById('log').innerText = "text";

document.getElementById('log').textContent = "text";

For Jquery:

$("#log").text("text");

$("#log").html("text");
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
16

HTML:

<div id="log"></div>

JS:

document.getElementById("log").innerHTML="WHATEVER YOU WANT...";
Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
2

I would suggest Jquery:

$("#log").html("Type what you want to be shown to the user");   
joe_young
  • 4,107
  • 2
  • 27
  • 38
1

If you are using jQuery and you want to add content to the existing contents of the div, you can use .html() within the brackets:

$("#log").html($('#log').html() + " <br>New content!");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="log">Initial Content</div>
joe_young
  • 4,107
  • 2
  • 27
  • 38