6
let btn = document.createElement("button");
btn.id = "submitBV"
let firstRow = 12
let lastRow = 59
btn.addEventListener("click", autoInput(firstRow, lastRow))

function autoInput(firstRow, lastRow){
    print()
}

It tells me Void function return value is used (screenshot).

After I add a return 1, it doesn't solve as well (screenshot2).

Cookie
  • 75
  • 1
  • 1
  • 5
  • 2
    [`.addEventListener()`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) expects a callback function as its second argument. You're currently invoking `autoInput()`, thus passing its return value to `.addEventListener()`. You can pass an [anonymous function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#the_function_expression_function_expression) which invokes `autoInput()` instead, `btn.addEventListener("click", () => autoInput(firstRow, lastRow))`. – khan May 19 '21 at 02:46
  • Or do `autoInput.bind(null, firstRow, lastRow)`. – quicVO May 19 '21 at 03:08

1 Answers1

9

Yes, because you are invoking the autoInput function which does not return something (void) valid. To handle this, to need to pass a function in order to be called when the click event is triggered by the button. In this case, you can add a simple function to do this.

btn.addEventListener("click", function() { autoInput(firstRow, lastRow); })

If you have a function which returns another function (if you are new to javascript, yes, it is possible) it may work fine, which is not your case on the autoInput function.

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194