Hello, i can’t seem to figure out what i’ve done wrong here.
My console.log does nothing, yet alert happens immediately upon declaration, so i suppose i’ve made a mistake in addEventListener or in my function …
When you add an event listener, you should be passing a function to it (in your case the check function). If you invoke that function inside the addEventListener, then you are no longer passing a function. Instead you are passing whatever that function returns. Perhaps if your check function was returning a function then this would be accepted but in this case, your check function isn’t returning anything.
Try just specifying check (without the arguments) in addEventListener.
Then inside of check, make it look up the value of the input
const text = document.getElementById("text-input");
const button = document.getElementById("check-btn");
function check() {
const input = document.getElementById("text-input").value
console.log(input);
alert("Alert!");
};
button.addEventListener("click", check);
But then - can i even pass a function with arguments into event listener, like what my initial code was intended to do? Can i declare it right there in event listener, or do i have to go through some “hoops”?
You don’t need to create another const input inside the check since you already made one earlier (you called it text) to get the element. You only need to get the value of the const text inside your check.
Also I would be careful about using const inside the function because this function is supposed to get whatever the user typed and what they type will change each time (so it is not a const. The input itself as an element is a const though)
As for your question. My understanding (I am also learning js so I am not an expert), is that trying to invoke the function will not return another function that addEventListener can use. Well, unless you change your code to make it return a function. But that is beyond the scope of this exercise and your learning so far.
You can also create an “anonymous” function within the call to addEventListener but again, that is beyond the scope. I think all they want from us in this project is to utilize the skills they taught us so far (so don’t over complicate it at this time)
But as said, that isn’t the correct thing to do here. You have access to the input element inside your check handler function, so you can just get the value property on that element inside the handler. The handler function does not need to be passed anything.
try not to post so much of your code here though. (unless you want us to read it so you can ask some question). We’re trying to keep the forum spoiler free.