I am trying to add a new textarea onClick for one of my buttons, however, I get the following error:
Uncaught TypeError: Failed to execute ‘insertBefore’ on ‘Node’: parameter 2 is not of type ‘Node’."
HTML:
<div class="form-group mt-2" id="we">
<label for="workField">Work Expereince</label>
<textarea
placeholder="Enter Text"
class="form-control weField"
rows ="3"></textarea>
</div>
<div class="container text-center mt-2" id="weAddButton">
<button onclick="addNewWeField()" class="btn btn-primary btn-sm">Add</button>
</div>
JS:
function addNewWeField() {
let newNode = document.createElement('textarea');
newNode.classList.add('form-control');
newNode.classList.add('weField');
newNode.classList.add("mt-2");
newNode.setAttribute("rows", 3);
newNode.setAttribute('placeholder','Text Here')
let weOb = document.getElementById("we");
let weAddButtonOb = document.getElementsByTagName('weAddButton');
weOb.insertBefore(newNode, weAddButtonOb);
}
Any help is appreicated!
2
Answers
It’s because "weAddButtonOb" is an HTMLCollection and not a Node. You probably want to get the id, instead of the tag name just like this:
}
You can also check the documentation about "insertBefore()" https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore .
anyway this code should work
.getElementsByTagName()
returnNodeList