onclick event is not calling the function

Asked

Viewed 123 times

1

The onclick event is not calling the desired function

HTML:

<!DOCTYPE>
<html>
<head>
        <script type="text/javascript" src="main.js"></script>
    <title>
        app
    </title>
</head>
<body>
    <form id="form">
    <input type="button" onclick="CriarLobby()"> criar lobbt </input>
    </form>
</body>
</html>

JAVASCRIPT

const { app, BrowserWindow} = require('electron');
const axios = require('axios');
const LCUConnector = require('lcu-connector');
const connector = new LCUConnector();
const https = require('https');
const agent = new https.Agent({
    rejectUnauthorized: false,
});

function createWindow(){
    const win = new BrowserWindow({
        width: 800,
    height: 600,
     webPreferences: {
      nodeIntegration: true
    }
    })

    win.loadFile('index.html')
}

app.whenReady().then(createWindow)

connector.on('connect', async(credentials) => {
    console.log('League Client started.');

const api = axios.create({
    baseURL: `https://127.0.0.1:${credentials.port}`,
     headers: {
            'content-type': 'application/json',
            'Authorization': `Basic ${Buffer.from(`${credentials.username}:${credentials.password}`)
            .toString("base64")}`,
        },
        httpsAgent: agent
    });

function CriarLobby(){
      api.post('/lol-lobby/v2/lobby', {
      queueId: 430,
    })
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });

}
}).start();

2 answers

0

The answer is simple friend, you can’t call the function because you declared the function within of function createWindow, i.e., it exists only within the scope of the createWindow function, so it gives the error CreateLobby não foi definido but this is solved if you declare the function outside the function scope createWindow.

0

Did you check the console through Browse? It may be that another part of the code is wrong and not the function call.

Try using this in your function call.

NOTE: The INPUT tag does not need to be closed.

Test only between the function call and the JS to see if the problem is really in it.

<form id="form">
    <button onclick="CriarLobby(this)"> criar lobbt </button>
</form>

<script>
 function CriarLobby(){

   alert("Função ok");
   console.log("Função ok");
 }
</script>
  • your code worked but when I replaced it with my original function (msm creating it in the script tag) the function was not called, I tried even using the following code: <form id="form">&#xA; <button id="botao"> criar lobby </button>&#xA;</form>&#xA;<script type="text/javascript">&#xA; if(document.getElementById('botao').clicked == true)&#xA;{&#xA; CriarLobby();&#xA;&#xA;}&#xA;&#xA;</script> but it didn’t work, I believe that the problem can only be in function

  • as to the console the result is this: image

  • I already answered hehe :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.