iori
iori

Reputation: 3506

How to display document.write in a specific div with specific id?

I want to display my document.write("something important");

into a specific div with specific id.

Im my HTML, I have a div <div class="col-lg-4" id="print"> ... </div>

In my JavaScript, I have a loop.

for(var i = 0; i < L; i++) {

  document.write(

    navigator.plugins[i].name +
    " | " +
    navigator.plugins[i].filename +
    " | " +
    navigator.plugins[i].description +
    " | " +
    navigator.plugins[i].version +
    "<br><hr><br>"
  );
}

What is the most efficient way to display them in my div ?

Upvotes: 3

Views: 11555

Answers (2)

blex
blex

Reputation: 25634

I would recommend not using document.write, especially after the page has loaded. It can lead to unexpected results. Just use this method:

document.getElementById('print').innerHTML = "something important";

If, however, you did not want to replace the whole innerHTML, you could append something to it:

document.getElementById('print').insertAdjacentHTML('beforeend', "something added");

Update

Here is an example with a loop:

var elem = document.getElementById('print'),
    L = navigator.plugins.length;

for(var i = 0; i < L; i++) {
  elem.insertAdjacentHTML('beforeend',
        navigator.plugins[i].name +
        " | " +
        navigator.plugins[i].filename +
        " | " +
        navigator.plugins[i].description +
        " | " +
        navigator.plugins[i].version +
        "<br><hr><br>"
  );
}

JS Fiddle Demo

Upvotes: 4

evandrolg
evandrolg

Reputation: 65

man! I think that what you want to do is update the content of the element, right? If so: document.getElementById('print').innerHTML = something important

I hope I have helped you

Upvotes: 1

Related Questions