Uncaught TypeError: Object is not an onclick function

I am getting the error "Uncaught TypeError: object is not a function" when clicking on a button on my web page.

Button code:

<button id="addUser" name="addUser" onclick="addUser()"> Add User </button>

Code for the addUser function

function addUser() {

    userValue = document.getElementById("userForAdmin");
    passValue = document.getElementById("passForAdmin");
    console.log(userValue.value);
    console.log(passValue.value);
}

      

Can anyone please point out my error? I guess this is probably the main mistake.

+3


source to share


1 answer


Use different names for function and element id. When you execute id="addUser"

, this creates a global variable window.addUser

that refers to the DOM element. This overrides the definition of a function with this name.



+5


source







All Articles