Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write inside a DIV box with javascript

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

like image 662
Shawn Avatar asked Sep 15 '13 03:09

Shawn


2 Answers

document.getElementById('log').innerHTML += '<br>Some new content!';
<div id="log">initial content</div>
like image 56
plalx Avatar answered Oct 10 '22 00:10

plalx


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"); 
like image 45
Muhammad Umer Avatar answered Oct 09 '22 23:10

Muhammad Umer