Search code examples
javascriptjsonjsplumb

How could I code a button to call the following JSON data?


I want to use the following button to get JSON data

<button>Get JSON data</button>

Using this JavaScript

    var t = jsPlumbToolkit.newInstance();

    var data = t.exportData({
      type:"myFormat",
      parameters:{
        importantNumber:34,
        somePrefix:"foo-"
      }
    });

How could I get the button to call this?


Solution

  • There are several ways of doing this in JavaScript. You could start with something as simple as defining a function in javascript and calling it each time the button is clicked:

    <button onclick="get_json_data();">Get JSON data</button>

    function get_json_data(){
        var t = jsPlumbToolkit.newInstance();
    
        var data = t.exportData({
            type:"myFormat",
            parameters:{ importantNumber:34, somePrefix:"foo-" } }
        );
        //Whatever you want to do here
    }
    

    I stress that maybe this is not the best way of doing this, but it's the easiest to understand. Hope this helps.