'Function not defined when function is defined? [closed]
I have the function defined as connectAcocunt() in my script.js file that I am trying to call from my index.html file on button press, that is imported with the line <script type = "text/javascript" src="script.js"></script>, but for some reason console is telling my that the function connectAcocunt() is not defined.
Ive been trying for 3 days now. Any help would be appreciated.
Here is my code as requested.
function connectAcocunt(){
//if the window has no ethereum property then prompt to install metamask else get run the getAccount function
if (typeof window.ethereum !== 'undefined') {
getAccount()
} else {
alert('Please make sure MetaMask in installed.');
}
}
async function getAccountBalance(){
web3 = new Web3(window.ethereum);
var accounts = await web3.eth.getAccounts();
console.log(web3.eth.getBalance(accounts[0]))
}
async function func(){
//the paremeter functions
const transactionParameters = {
nonce: '0x00', // ignored by MetaMask
gasPrice: '0x55F0', // customizable by user during MetaMask confirmation.
gas: '0x55F0', // customizable by user during MetaMask confirmation.
to: '0xfa7679267a57b463c95fc208d3eec0635b8238cc', // Required except during contract publications.
from: ethereum.selectedAddress, // must match user's active address.
value: '0x58D15E17628000', // Only required to send ether to the recipient from the initiating external account.
chainId: '0x3', // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
};
const txHash = await ethereum.request({
method: 'eth_sendTransaction',
params: ['transactionParameters'],
})
}
async function getAccount() {
//selecting the show account class in main html
const showAccount = document.querySelector('.showAccount');
//getting accounts from metamask
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
//selecting the first one
const account = accounts[0];
//setting the show account class text as the frist metamask acount
showAccount.innerHTML = account;
//setting the connect button text to the account adress
document.getElementById("connect").innerHTML = account.substr(0, 3)+ '...'+ account.substr(38, 42);
}
<!DOCTYPE html>
<html>
<head>
<title>Optimistic Apes</title>
<script type = "text/javascript" src="script.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<button id="connect" class="button" onclick="connectAcocunt();">Connect</button>
<button class="button" onclick="func();">Mint NFT</button>
<h2>Account: <span class="showAccount"></span></h2>
</body>
</html>
and my js file
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
