Context with passing parameter in JavaScript
There is another solution to the problem. Let's
make the function child take a parameter:
function child(param) {
// here will be a code
}
And when calling this function, we will
pass this into it:
function parent() {
child(this); // pass the this as parameter
function child(param) {
// the param variable contains the passed content of this
}
}
Since the call to child is carried out in the
parent function, the passed this points out
what we needed. Then the this gets into the
param parameter and will be available inside
the function in this form.
Here is the final code:
let elem = document.querySelector('#elem');
elem.addEventListener('blur', parent);
function parent() {
child(this); // passes the this as parameter
function child(param) {
console.log(param.value); // prints the value of the input
}
}
Take the code from the previous task and fix the code problem using the second method you learned.