Skip to main content
Onsightclimb
Participating Frequently
February 18, 2016
Question

Checking last checkbox value

  • February 18, 2016
  • 1 reply
  • 459 views

I have a problem and need some help, please.

I have 40 groups of checkboxes,in each group is 7 checkboxes , every group has unique name and value.

User can check random number of groups, in range betwen 1 - 40. Here is my problem. Can I use javascript to get value of LAST checked group?

If user check for example 11 groups in row...i need to get value of eleventh group. Am I making any sence? Is this possible?

TNX for any help.

This topic has been closed for replies.

1 reply

Inspiring
February 18, 2016

JavaScript only knows fields by name. So you need to figure out how to name your fields in such a manner that arranges them in row order.

I would look at using hierarchical field names with the same parent name and each child goup is an ascenting number. One could then check each group and track the last group that does not have a value of "Off".

Onsightclimb
Participating Frequently
February 19, 2016

I have diferent names of groups. First is "r1", second "r2"...and so on all way to "r40". I dont know how to put together script, that will track the last group with value, that is not "OFF"? Can you show me?

Inspiring
February 19, 2016

One way would be to use a script that's called in the Mouse Up event of each check box that sets the value of a (hidden) text field to the value of the check box. It could be something like:

// Function in a document-level script

function setLastVal() {

    // Get the value of the check box that triggered this function

    var val = event.target.value;

    // Set the value of the text field to this value

    if (val !== "Off") {

        getField("LAST_CB_VAL").value = val;

    }

}

where "LAST_CB_VAL" is the name of the text field that holds the most recently selected check box value. To retrieve it, just get the value of this text field. You would have to call this function in the Mouse Up event of each check box like this:

// Mouse Up script of checkbox

setLastVal();

It would be tedious to manually do this, but you can use the following script to automate it:

// Set the Mouse Up script for all of the check boxes

var sJS = "getField(\"LAST_CB_VAL\").value = val;";

for (var i = 1; i <= 40; i += 1) {

     getField("r" + i).setAction("MouseUp", sJS);

}

This can either be the Mouse Up script of a temporary button or can be entered, selected, and run in the interactive JavaScript console (Ctrl+J).