4

I receive the error "Uncaught TypeError: object is not a function" when clicking on a button in my webpage.

The button code is:

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

The code for function addUser is

function addUser() {

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

Can anyone point out my error? My guess is its likely a basic mistake.

Val Opfermann
  • 107
  • 2
  • 5

1 Answers1

6

Use different names for the function and the element ID. When you do id="addUser", that creates a global variable window.addUser that refers to the DOM element. This is overriding the function definition with that name.

Barmar
  • 741,623
  • 53
  • 500
  • 612