[Ethereum] How to pass a parameter to a function in web3

jqueryweb3js

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?

Best Answer

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")});
Related Topic