Getting values of an object into a event

I trying to make a script which has a lot of "ButtonObject"s, which contain p5 buttons and some data. I want to use this data inside of a event (like "mousePressed") which is registered to a p5 button of the respective "ButtonObject".

The problem is, that when the button is pressed the console does output 'undefined' instead of 'Text'.

Is there a simple way to do this or is it impossible?


var button;

function setup() {
    button = new ButtonObject();


function ButtonObject() {
    this.but = createButton('Button');
    this.txt = 'Text';

    this.but.mousePressed( function ( ) {



        <meta charset="UTF-8">
        <script language="javascript" type="text/javascript" src="libraries/p5.js"></script>
        <script language="javascript" src="libraries/p5.dom.js"></script>
        <script language="javascript" type="text/javascript" src="sketch.js"></script>
        <script language="javascript" type="text/javascript" src="button.js"></script>
        <style> body {padding: 0; margin: 0;} </style>

Thanks in advance!

Sign In or Register to comment.