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 ?
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 ?
Share Improve this question edited Mar 18, 2015 at 19:05 iori asked Mar 18, 2015 at 18:46 ioriiori 3,50615 gold badges49 silver badges79 bronze badges2 Answers
Reset to default 4I would remend 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
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