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

How to give all duplicate checkbox names a unique name?

Enthusiast ,
Dec 23, 2021 Dec 23, 2021

Copy link to clipboard

Copied

I found this article that talks about rename all duplicate field names. 

 

    for (var i=0;i<200;i++)
    {
    try{
    var ts=this.getField("Name1."+i).textSize;
    var tf=this.getField("Name1."+i).textFont;
    var rct=this.getField("Name1."+i).rect;
    var pg=this.getField("Name1."+i).page;
    var f=this.addField("name."+i,"text",pg,rct);
    f.textSize=ts;
    f.textFont=tf;
    }catch(e){break}
    }

this.removeField("Name1");

 

I believed this script is for text box. I need to rename all my duplicate checkbox names. Do I sipmly changed the "text" to "checkbox" for checkbox field instead of text box field? I tried that and it didn't really work. I no longer can click on the checkbox once I ran this script. Any suggestion is much appreciated.

TOPICS
JavaScript

Views

3.8K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jan 07, 2022 Jan 07, 2022

Use this code:

 

for(var i = 0; i< 61; i++){
    if(this.getField("T Neej."+i)!= null){
        var ts=this.getField("T Neej."+i).textSize;
        var tf=this.getField("T Neej."+i).textFont;
        var rct=this.getField("T Neej."+i).rect;
        var pg=this.getField("T Neej."+i).page;
        var f=this.addField("checkBox"+i,"checkbox",pg,rct);

        f.textSize=ts;
        f.textFont="ZapfDingbats";
        this.removeField("T Neej."+i);
    }
}

Votes

Translate

Translate
Community Expert ,
Dec 23, 2021 Dec 23, 2021

Copy link to clipboard

Copied

Yes, that should do it. I would remove the try-catch clause, though, as it's hiding any errors you might get while running this code. Don't set the textFont property, though, if you create check-boxes. It might screw them up.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

I could not get it to work. After I ran the code below, it did removed the existing checkbox and rename them; however, it's no longer a check box. I can no longer click the field to put a cross on it any more.

for (var i=0;i<200;i++)
    {
    var ts=this.getField("Name1."+i).textSize;
    var tf=this.getField("Name1."+i).textFont;
    var rct=this.getField("Name1."+i).rect;
    var pg=this.getField("Name1."+i).page;
    var f=this.addField("checkbox."+i,"checkbox",pg,rct);
    f.textSize=ts;
    f.textFont=tf;
    }
this.removeField("Name1.");

Votes

Translate

Translate

Report

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 ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

Do not set the textFont of the check-boxes to anything else but the default value ("ZapfDingbats"). If you do that they won't work properly.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

Same issue but with an error this time.

for(var i = 0; i< 200; i++){
        var ts=this.getField("Name1."+i).textSize;
        var rct=this.getField("Name1."+i).rect;
        var pg=this.getField("Name1."+i).page;
        var f=this.addField("checkBox"+i,"checkbox",pg,rct);
        f.textSize=ts;
        this.removeField("Name1.");
}

TypeError: this.getField(...) is null
2:Console:Exec
undefined

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

That means there isn't a field with the name you're specifying.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

If I highlight this code, it does show the correct font name.

2Charlie_0-1641397818803.png

 

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

You should add a command to print out the value of i when the error happens. That way you could locate the missing field.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

I also tried:

for(var i = 0; i< 200; i++){
        var ts=this.getField("Name1."+i).textSize;
	var tf=this.getField("T Neej."+i).textFont;
        var rct=this.getField("Name1."+i).rect;
        var pg=this.getField("Name1."+i).page;
        var f=this.addField("checkBox"+i,"checkbox",pg,rct);
        f.textSize=ts;
	f.textFont="ZapfDingbats";
        this.removeField("Name1.");
}

and I got the same error:

TypeError: this.getField(...) is null
2:Console:Exec
undefined

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

Have you 200 checkboxes?

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

I don't know the exact checkboxes but it's like around 80-90 checkboxes. 

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

But your counter runs to 200, so of course you're going to get an error...

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

You should check the field:

if (this.getField (" ... ") != null) {

...

}

 

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

So, here's my attempt to use "if" to check for not null.

 

for(var i = 0; i< 96; i++){
    if(this.getField("Name1")!= null){
        var ts=this.getField("Name1"+i).textSize;
        var tf=this.getField("Name1"+i).textFont;
        var rct=this.getField("Name1"+i).rect;
        var pg=this.getField("Name1"+i).page;
        var f=this.addField("checkBox"+i,"checkbox",pg,rct);

        f.textSize=ts;
        f.textFont="ZapfDingbats";
        this.removeField("Name1");
    }
}

 

And the error is:

TypeError: this.getField(...) is null
3:Console:Exec
undefined

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

This code should show more than just one result of "10" but it's not.

2Charlie_0-1641403048269.png

 

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

Okay, this shows the correct number of checkboxes. But I'm still unable to get the "added" checkboxes to function like a checkbox. I'm unable to click on it.

2Charlie_0-1641403578005.png

 

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

What can you see when you look at the properties of the checkboxes?

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

The issue seems to be this line: var f=this.addField("checkBox."+i,"checkbox",pg,rct);

This line: this.removeField("Name1."); removes the current checkbox but the add line above failed to add additional checkboxes. When the code finished running, there is only one checkbox.0.

2Charlie_0-1641405913124.png

 

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

The name of the checkbox is "Name2".

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

I'm sorry, all the name of the checkboxes are just "Name". I renamed that particular checkbox for testing.

Votes

Translate

Translate

Report

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 ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

It's very difficult to help you because your descriptions continue to change. Unless you can share the actual file I can't help you any further with this. Maybe someone else can.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 07, 2022 Jan 07, 2022

Copy link to clipboard

Copied

Attached is the test PDF I'm playing with. Below is the code I'm using.

for(var i = 0; i< 61; i++){
    if(this.getField("T Neej.")!= null){
        var ts=this.getField("T Neej."+i).textSize;
        var tf=this.getField("T Neej."+i).textFont;
        var rct=this.getField("T Neej."+i).rect;
        var pg=this.getField("T Neej."+i).page;
        var f=this.addField("checkBox"+i,"checkbox",pg,rct);

        f.textSize=ts;
        f.textFont="ZapfDingbats";
        this.removeField("T Neej.");
    }
}

 

Votes

Translate

Translate

Report

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 ,
Jan 07, 2022 Jan 07, 2022

Copy link to clipboard

Copied

Use this code:

 

for(var i = 0; i< 61; i++){
    if(this.getField("T Neej."+i)!= null){
        var ts=this.getField("T Neej."+i).textSize;
        var tf=this.getField("T Neej."+i).textFont;
        var rct=this.getField("T Neej."+i).rect;
        var pg=this.getField("T Neej."+i).page;
        var f=this.addField("checkBox"+i,"checkbox",pg,rct);

        f.textSize=ts;
        f.textFont="ZapfDingbats";
        this.removeField("T Neej."+i);
    }
}

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 07, 2022 Jan 07, 2022

Copy link to clipboard

Copied

Thank you so much for the help! That solves the issue.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jan 05, 2022 Jan 05, 2022

Copy link to clipboard

Copied

Is there a way to get the exact number of checkboxes based on the getField("Name1")?

Votes

Translate

Translate

Report

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