Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick event won't recognise function

Using Javascript. I'm getting an error when the onclick of a button fires (by pressing the button). The error is:

Uncaught TypeError: accept is not a function at HTMLInputElement.onclick

Here's the HTML of the button (and its neighbour):

<input type="button"  value="Accept Offer" onclick="accept()" />
<input type="button"  value="Cancel" onclick="cancelAccept()" />

The Cancel button works fine.

Here's the accept JS function:

function accept() {
    //check that user logged in
    api.me()
    .then( (res) => {   //logged in, accept offer
        acceptOffer(res.user);
    }, (err) => {
        console.log("error:",err);
        alert("Authentication error. Please logout and in again");
        cancel();
    });
}

The javascript is imported(?) in the head tag at the top of the html page. api.me() is a call to a library.

like image 289
erv Avatar asked Jun 04 '26 08:06

erv


1 Answers

It seems the name accept is causing trouble. See this jsfiddle.

Rename your function and put it in the global scope.

like image 76
Decay42 Avatar answered Jun 05 '26 22:06

Decay42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!