0

I have the following problem to solve, when I start writing text in input, this one should turn red. Unfortunately, for unknown reasons, the variable is unidentified. Perhaps the problem is that js is not able to define the variable as input. I don't understand something here. Please help.

var input_Login = document.getElementsByTagName('input')[0];

console.log(input_Login);

function inputCheck(input_Login){
    input_Login.style.color='pink';
}

var doit = input_Login.addEventListener('oninput', inputCheck);
Gerard
  • 13,747
  • 5
  • 24
  • 44
  • 1
    The name of the event is `input`, not `oninput`. – Barmar Apr 27 '20 at 17:49
  • After you've fixed the unidentified variable and the event name, there's still one problem left. `input_Login` in the event handler is not what you think it is. It is the event object passed automatically to the event handlers. You've to remove it from the argument list of the function definition, it shadows the outer `input_Login` variable. Values to the functions arguments are passed when the function is called, not when the function is defined. – Teemu Apr 27 '20 at 17:54
  • I still did not solve my first problem. Why input_Login veriable is undefined? In last line I understand input_Login as a object which i have to interact to start my function. But why still appear input_Login veriable is undefined? – Kamil Krzyżański Apr 27 '20 at 18:25

1 Answers1

0

You need to use the event input and the keyword thisin your function, see the example below:

var input_Login = document.getElementsByTagName('input')[0];

console.log(input_Login);

function inputCheck(input_Login){
    this.style.color='pink';
}

var doit = input_Login.addEventListener('input', inputCheck);
<input type="text">
user2314737
  • 21,279
  • 16
  • 81
  • 95