Ad

How Does One Add A
Above A Hidden

- 1 answer

I am trying to add a new div before the hidden div ValidationSummary1

var existingNode = document.getElementById('ValidationSummary1')
var addin = document.createElement('div')
addin.innerHTML = "Hello World!"
existingNode.insertBefore(addin,existingNode[0])
<div>
    <div id="ValidationSummary1" class="Error" style="color:Red;display:none;">
    </div>
</div>

Any help would be greatly appreciated.

Ad

Answer

You can set the attribute id to the element using which you can set the textContent property. You also have to target the parentNode of the newly inserted node:

var existingNode = document.getElementById('ValidationSummary1');
var addin = document.createElement('div');
addin.id = 'newDiv';
existingNode.parentNode.insertBefore(addin,existingNode);
document.getElementById('newDiv').textContent = 'hello world';
<div>
    <div id="ValidationSummary1" class="Error" style="color:Red;display:none;">
    </div>
</div> 

Ad
source: stackoverflow.com
Ad