Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Get Name of checkbox field from click

Community Beginner ,
Mar 01, 2022 Mar 01, 2022

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.

TOPICS
How to , JavaScript , PDF forms
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
Mar 01, 2022 Mar 01, 2022

It's event.target.name, actually.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2022 Mar 02, 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
}


PDF Acrobatic, InDesigner & Photoshoptographer

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2022 Mar 01, 2022

It's event.target.name, actually.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2022 Mar 02, 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
}


PDF Acrobatic, InDesigner & Photoshoptographer
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2022 Mar 02, 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);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2022 Mar 02, 2022

This is a bit dangerous to use, because if you use this method to look for "text-1", for example, it will match both "text-1", as well as "text-10", "text-11", "text-115", etc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2022 Mar 02, 2022

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

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


PDF Acrobatic, InDesigner & Photoshoptographer
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2022 Mar 02, 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") {

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2022 Mar 02, 2022
LATEST

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;
}
}
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines