Skip to main content
Participant
March 1, 2022
Answered

Get Name of checkbox field from click

  • March 1, 2022
  • 3 replies
  • 1580 views

Hello All, 

First time post here, but I couldn't find anything on this particular subject so I felt I needed to reach out.

 

I have a document with hundreds of checkboxes. I'm trying to instruct these checkboxes to run a document level code on mouseup, but in order for it to run correctly, I need the name of the checkbox field being checked/unchecked. I am currently doing this by looping through all fields in the document, but it's causing the document to be really slow. 

I've tried using event.source.name and other variants, but no luck. 

Any help would be appreciated.

This topic has been closed for replies.
Correct answer JR Boulay

// name of active field
var strFldName = event.target.name;

// if field's name contain "text-12"
if (/text-12/.test(strFldName)) {
// your script here
}

3 replies

JR Boulay
Community Expert
Community Expert
March 2, 2022

try67 is right, you should use this instead, "\b" means "word limit":

if (/\btext-12\b/.test(strFldName)) {

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
March 2, 2022

Not sure why you need a regular expression at all... You can just do a simple comparison, like this:

if (event.target.name=="text-12") {

Participant
March 2, 2022

Many thanks to you both for your feedback on this. 

I took the time to rename all of the checkboxes before starting this, but I appreciate you two bringing this point up. 

This is really a simple bit of code to highlight a premade selection of text when the corresponding checkbox is selected (to make selections more easily visible). In case anyone else comes up on a similar problem, I'll include my code below.

// code in each checkbox

var myName = event.target.name;
var myPage = this.pageNum;
hiLighter(myName, myPage);

 

//document level javascript function

function hiLighter(myName, myPage)
{
var curField = this.getField(myName);
if (curField.type == "checkbox") {
    var hiLite = this.getAnnot(myPage, "HL."+myName);
    if (hiLite != null){
    if (curField.value == "Yes") hiLite.hidden = false;
    if (curField.value == "Off") hiLite.hidden = true;
}
}
}

 

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
March 2, 2022

// name of active field
var strFldName = event.target.name;

// if field's name contain "text-12"
if (/text-12/.test(strFldName)) {
// your script here
}

Acrobate du PDF, InDesigner et Photoshopographe
Participant
March 2, 2022

Thanks very much, JR. 

It wouldn't work until I added an additional line passing the "event.target.name" to a variable.

// I was trying this...
functionName(event.target.name);

// This is what worked

var myName = event.target.name
functionName(myName);

 

try67
Community Expert
Community Expert
March 1, 2022

It's event.target.name, actually.