Sid
Sid

Reputation: 613

Javascript "input" event value is undefined

I have an Electron app that needs to monitor an input text field on a form. The HTML looks like this:

<input type="text" class="cloneProjectName" id="outputProjectName" value="" >

I add an event listener for the element and console log what I think should be the typed data from the input:

    projectNameControl.addEventListener("input", function (event) {
    console.log(event.value)
})

All I see in the console is "undefined."

I would appreciate any input, I have searched and searched without finding an answer. Sid

Upvotes: 0

Views: 2800

Answers (1)

Scott Marcus
Scott Marcus

Reputation: 65855

It's not the value of the event that you want (events don't have a value). It's the value of the element that triggered the event that you want and that element can be referenced with this or event.target.

Also, make sure that your JavaScript projectNameControl variable correctly references the input.

let projectNameControl = document.getElementById("outputProjectName");
projectNameControl.addEventListener("input", function (event) {
    console.log(this.value, event.target.value);
})
<input type="text" class="cloneProjectName" id="outputProjectName" value="" >

Upvotes: 2

Related Questions