JavaScript document.write to display content on same page. document.write image display

I have a doubt with javascript document.write method. Mostly when I use document.write() it shows me the content written with the method in a different page. For instance, if I write the command like this, document.write("Hello, My name is Sameeksha"); then the execution of this line takes me to a different document on the same page. I want to be able to append the message on the same page, with other elements of the page. For example, if I have text boxes and buttons on the page and I want the text with document.write to appear under all the content of the page or on a particular section of a page. Please suggest what can be done to get the output in this way? As, this way it will be really easy to create dynamic HTML content. Thank you so much for your time.

Regards, Sameeksha Kumari

Answer:1

document.write is basically never used in modern Javascript.

Whan you do instead is to create explicit DOM elements and append them to the document in the place you want. For example

var x = document.createElement("div");  // Creates a new <div> node
x.textContent = "Hello, world";         // Sets the text content
document.body.appendChild(x);           // Adds to the document

Instead of appending to the end you can also add child nodes to any existing node. For example:

function addChatMessage(msg) {
    var chat = document.getElementById("chat"); // finds the container
    var x = document.createElement("div");
    x.textContent = msg;
    chat.appendChild(x);
}
Answer:2

I want to open another searchpage.html html page with slide effects for which i used anchor tag. <a id="add" href="searchpage.html" class="show_hide">Click</a> And js: $(document)....

I want to open another searchpage.html html page with slide effects for which i used anchor tag. <a id="add" href="searchpage.html" class="show_hide">Click</a> And js: $(document)....

I am reading a book about single-page applications and at some point there is this for...in loop: KEYVAL: for(key_name in arg_map){ if(arg_map.hasOwnProperty(key_name)){ if(key_name....

I am reading a book about single-page applications and at some point there is this for...in loop: KEYVAL: for(key_name in arg_map){ if(arg_map.hasOwnProperty(key_name)){ if(key_name....

I have following html on my page: <div class="group-one" > <p><span id="handle" draggable="true">::</span> click me</p> </div> <div class="group-two" ...

I have following html on my page: <div class="group-one" > <p><span id="handle" draggable="true">::</span> click me</p> </div> <div class="group-two" ...

I would like to create a timeout on each google marker's DROP animation, but I think the closure code for the marker and the array item are conflicting. I do not know too much about closures and am ...

I would like to create a timeout on each google marker's DROP animation, but I think the closure code for the marker and the array item are conflicting. I do not know too much about closures and am ...

  1. animation markers google maps
  2. custom animation google maps markers