简体   繁体   中英

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

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="" > 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM