I'm new and looking to learn. I have a js code in my file that takes care of showing / hiding the password field. Everything works fine but I notice an error in the console which is the following: Uncaught TypeError: Cannot read properties of undefined (reading 'parentNode').
Why am I getting this? Am I doing something wrong ?
This is my code, basically the error occurs in the function line.
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
/* New Div */
const Adiv = document.createElement("div");
Adiv.setAttribute('class', 'input-group-delete');
const inputs = document.querySelectorAll("#plugin_delete_me_shortcode_password");
const lastInput = inputs[inputs.length - 1]
insertAfter(lastInput, Adiv);
inputs.forEach(inp => Adiv.append(inp))
/* New Input */
var InputA = document.createElement("input");
InputA.setAttribute('type', 'checkbox');
InputA.setAttribute('class', 'switch');
InputA.setAttribute('id', 'field_3');
InputA.setAttribute('onclick', 'showPassword("plugin_delete_me_shortcode_password")');
var input = document.getElementById("plugin_delete_me_shortcode_password");
insertAfter(input, InputA);
/* New Label */
var newL = document.createElement("label");
newL.setAttribute('for', 'field_3');
newL.setAttribute('class', 'fa');
var input = document.getElementById("field_3");
insertAfter(input, newL);