I use this easy code to display the public variable of a smart contract:

window.addEventListener('load', function () {

    if(typeof Web3 !== 'undefined'){
        var web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/MY_KEY"));
        console.log('Found web3!');
        console.log("No web3!");

    function startApp(){

        var SalesABI = web3.eth.contract([{ 
          // loads of lines
        var SalesAddress      = 'address';
        var SalesContract     =  SalesABI.at(SalesAddress);

    displayETHRaised(SalesContract, web3);


function displayETHRaised(_SalesContract,_web3) {
    _SalesContract.tokenRaised((_err,_resp) => {
        if (_err != null) {
        } else {
            $('#DOM_ID').innerHTML = _web3.fromWei(_resp, "ether").toString(10);

I do not get the value of the public var and I don't get an error nor warning.

Everything seems to be fine but I do not get the value of the public variable tokenRaised.

  • Do you mean your callback gets called but there's an error in _err? Or it gets called with no error but the value in _resp is wrong? Or your callback never gets invoked? Are there any errors in the console? – user19510 Jan 11 '18 at 5:04

Try to use web3js-raw which is a simple wrapper around web3js.

| improve this answer | |

Ok, I solved it the next day. The bug was somewhere else.

If someone want to use the code it's fine except do not use .innerHTML on the jQuery object.

| improve this answer | |
  • yep, u should use instead ".html('aabbcc')" – Elya Livshitz Jul 29 '18 at 14:39

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.