简体   繁体   中英

For "onevent change" in javascript I am trying to access the parameter of the function, but getting an error as "undefined"

I have a code as below:

<input type="text" id="GAMES" name="OwnGames" width="100%" onChange="ValidateText(document.getElementById('GAMES'))";

Javascript:

function ValidateText(e,Games) {
    console.log(Games.id);
    var text_value=e.target.value;
    console.log(text_value);
 }

Console logs is correctly giving event e, but giving an error as Games is undefined.

After removing argument Games, I am getting an error as e is undefined.

We have an easy way to access all the element property and values. Please refer the snippet.

 function ValidateText(event) { var text_value= event.target.value; var id = event.target.id; console.log(`value : ${text_value}, id : ${id}`) }
 <input type="text" id="GAMES" name="OwnGames" width="100%" onChange="ValidateText(event)" >

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