quick question, i know we can change the content of a
<div id="whatEverId">hello one<div> by using:
now, is there a way I can ADD stuff to the div instead of replacing it??? so i can get
<div id="whatEverId">hello one hello two<div>
(using something similar of course)
<div id="whatever">hello one</div> <script> document.getElementById("whatever").innerHTML += " hello two"; </script>
document.getElementById("whatEverId").innerHTML = document.getElementById("whatEverId").innerHTML + "hello two" + document.getElementById("whatEverId").innerHTM ;
What jcomeau_ictx suggested is an inefficient way of editing the innerHTML. Check Ben cherry's PPT http://www.bcherry.net/talks/js-better-faster
Notice that using
element.innerHTML += 'content' would empty
textareas to their default, blank state, unclick checkboxes etc. because the whole
innerHTML would be reinterpreted by the browser.
If you need to keep the state, you'd need to create a new element (a
<span> for instance) and append it to the current element, as in:
let newElement = 'span' newElement.innerHTML = 'new text' document.getElementById('oldElement').appendChild(newElement)
If you are appending, you can just change your = to a +=
document.getElementById("whatEverId").innerHTML += 'hello two';
document.getElementById("whatEverId").innerHTML = 'hello two' + document.getElementById("whatEverId").innerHTML;
You can do it by appending div string like this..
document.getElementById('div_id').innerHTML += 'Hello Two';
©2020 All rights reserved.