# Using forEachField function in form field

**URL:** <https://pdfjs.community/t/using-foreachfield-function-in-form-field/499>\
**Category:** Technical Support\
**Tags:** pdfjs-express\
**Created:** [February 15, 2021, 8:11am UTC](https://pdfjs.community/t/using-foreachfield-function-in-form-field/499 "2021-02-15T08:11:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aman](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@aman](https://pdfjs.community/u/aman)\
**Post date:** [February 15, 2021, 8:11am UTC](https://pdfjs.community/t/using-foreachfield-function-in-form-field/499/1 "2021-02-15T08:11:50Z")

</div>

**PDF.js Express Version**

**Detailed description of issue**  
I was trying out pdfjs express specially for its form mode and tried to get details of all the fields of [this pdf file](https://www.irs.gov/pub/irs-pdf/f8879.pdf) but while using forEachField I am getting everything as empty. Also when I consoled the field I got a response but it did not specify all the fields, I will include the screenshot of the response.

 ![Screenshot from 2021-02-15 13-35-40](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/ebdefeac1494195eef23f461fc438bb094a8b07a.png)

**Expected behaviour**  
I also tried using the same function on one of your demo pdf and it seems to work but not on the pdf that I have added the link for.

**Link to document**

> **[f8879.pdf](https://www.irs.gov/pub/irs-pdf/f8879.pdf)**
>
> 88.95 KB

**Code snippet**  
CODE 1-\>

```javascript
WebViewer(
      {
        path: '/webviewer/lib',
        initialDoc: '/files/f8879.pdf',
      },
      viewer.current,
    ).then(instance => {
      const { docViewer, annotManager } = instance;
    
      docViewer.on('documentLoaded', () => {
        docViewer.getAnnotationsLoadedPromise().then(() => {
          var stack = [];
          const fieldManager = annotManager.getFieldManager();
          fieldManager.forEachField(function(field) {
         stack.push(field); // Push root fields
         console.log(field)
       });
        while (stack.length > 0) {
         var current = stack.pop();
         if (current.isTerminal()) {
           // Work with terminal/leaf fields
         }
         else {
           // Traverse children
           stack = stack.concat(current.children);
         }
       }
        });
      });
    });

```

CODE 2 →

```javascript
WebViewer(
      {
        path: '/webviewer/lib',
        initialDoc: '/files/f8879.pdf',
      },
      viewer.current,
    ).then(instance => {
      const { docViewer, annotManager, fieldManager } = instance;
    
      docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(() => {
      // iterate over fields
      const fieldManager = annotManager.getFieldManager();
      fieldManager.forEachField(field => {
        console.log(field.getValue());
        field.setValue('new value');
      });
    });
  });
    });

```

---

<div class="post-metadata">

**Author:** ![Logan](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/logan/32/13_2.png) [@Logan](https://pdfjs.community/u/Logan)\
**Post date:** [February 17, 2021, 3:53pm UTC](https://pdfjs.community/t/using-foreachfield-function-in-form-field/499/2 "2021-02-17T15:53:43Z")

</div>

Hey there!

You need to make sure to iterate over any children like you’re doing in your second code sample (although there are a few errors there).

This code should do it for you:

```javascript
  const { docViewer, annotManager } = instance;

  const fieldManager = annotManager.getFieldManager()
    
  docViewer.on('documentLoaded', () => {
    docViewer.getAnnotationsLoadedPromise().then(() => {
        var stack = [];
        fieldManager.forEachField(function(field) {
          stack.push(field); // Push root fields
        });
        while (stack.length > 0) {
          var current = stack.pop();
          if (current.isTerminal()) {
            // Work with terminal/leaf fields
            console.log(current) // current is a `Field` class
          }
          else {
            // Traverse children
            stack = stack.concat(current.children);
          }
        }
    });
  });

```

Thanks!  
Logan
