Skip to main content
xtd8865
Known Participant
January 2, 2018
Question

JS reference to the parent object

  • January 2, 2018
  • 2 replies
  • 439 views

Hi,

I have a grid of tiles. The tiles are called: box_01, box_02, etc. I'm writing a JS script to access values that are under variables: box_01_value, box_02_value, etc.

Is there any way to reference the object to get the name of the variable I want the value from, eg.

When a user clicks on box: "box_01", the script would get the value from variable "box_01_value". Obviously I could do it specifically for each box, but that's not what programming is about. Is there a way of making it more abstract generic so the code is the same for each box, eg:

alert(self + "_value");

Additionally, is there any update on the js loop problem with captivate js window? As read on this forum about the < sign, I've redone the loop to use > but still no luck. The code works fine outside Captivate. In the end I had to hard code it (which again defeats the purpose of writing code a bit).

var L = ['A', 'B', 'C', 'D'];

var suff = '_value';

var grid = [];

var min = 1;

var max = 5;

for (var i = L.length-1; i>=0; i--) {

        for (var j = L.length-1; j>=0; j--) {

                grid.push(L + String(j+1) + suff)

        }

}

alert(grid);

The output is:

[ 'D4_value',

  'D3_value',

  'D2_value',

  'D1_value',

  'C4_value',

  'C3_value',

  'C2_value',

  'C1_value',

  'B4_value',

  'B3_value',

  'B2_value',

  'B1_value',

  'A4_value',

  'A3_value',

  'A2_value',

  'A1_value' ]

Thank you.

    This topic has been closed for replies.

    2 replies

    Jeremy Shimmerman
    Participating Frequently
    January 3, 2018

    There is probably a much better way of doing this but here's what I've done in the past.  Under the ID of each box add 'aa' to the start and write the values as part of the ID.  For example if box D4 has a value of 12 it would be 'aaD4_12'

    window.addEventListener("mousedown", function(e) {

    cpObj = e.target.id.substring(0,2);

    output = e.target.id.substring(2);

    if(cpObj == "aa" ) {

    console.log(output);

    }

    }

    );

    The reason for the "aa" business is that if they click anywhere else, for example the background, it will display the id for that.  Also, if you want the output to be an integer then use parseInt(). 

    xtd8865
    xtd8865Author
    Known Participant
    January 4, 2018

    Thanks - This should get me started.

    Legend
    January 3, 2018

    when you say "grid of tiles" what are we talking about?  Click boxes, Fluid boxes, Smart Shape Buttons???

    xtd8865
    xtd8865Author
    Known Participant
    January 3, 2018

    They are Smart Shape Buttons