I have a button that executes a function in a solidity contract like this:

<a href="#" onclick="solidityFunction()" class="btn btn-primary btn-block">Execute Function</a>

The solidityFunction() might take one parameter. In the web3 Javascript I have it like the following:

window.solidityFunction = function () {
    contract.deployed().then(function (contractInstance) {
        contractInstance.solidityFunction({from: web3.eth.accounts[0], gas:1000000, value: web3.toWei(0.01, "ether")})

How do I pass a parameter from the onclick="solidityFunction()" that I then use in the window.solidityFunction part?


If you what to pass a name as string or any other value then you can use jquery like this

var myParameter= $("#someID").val();

When some clicks the button you get the name or number on which the person has clicked. After that you pass this variable (which is string in this case) as the parameter to the function.

contractInstance.solidityFunction(myParameter,{from: web3.eth.accounts[0], gas:1000000, value: web3.toWei(0.01, "ether")});

you can pass the multiple parameter with coma separator

contractInstance.methods.solidityFunction(parameter...,{from: web3.eth.accounts[0], gas:1000000, value: web3.toWei(0.01, "ether")});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.