# \<foreach>

## \<foreach> <a href="#foreach" id="foreach"></a>

The `<foreach>` element allows a VoiceXML application to iterate through an ECMAScript array and to execute the content contained within the `<foreach>` element for each item in the array.

### Attributes

<table><thead><tr><th width="165.33333333333331">Name</th><th width="145">Data type<select><option value="e34e5ffcdba4485da7321672d597a4b8" label="String" color="blue"></option><option value="1bf311113b52477bbf49042da8980b32" label="Integer" color="blue"></option><option value="8eb5a21a18f74ceb8a508ba48479c69f" label="Boolean" color="blue"></option></select></th><th>Description</th></tr></thead><tbody><tr><td>array</td><td><span data-option="e34e5ffcdba4485da7321672d597a4b8">String</span></td><td>An ECMAScript expression that must evaluate to an array; otherwise, an error.semantic event is thrown.</td></tr><tr><td>item</td><td><span data-option="e34e5ffcdba4485da7321672d597a4b8">String</span></td><td>The variable that stores each array item upon each iteration of the loop. A new variable will be declared if it is not already defined within the parent's scope.</td></tr></tbody></table>

### Notes

Both “array” and “item” must be specified. Please refer to the [VoiceXML 2.1 Draft](http://www.w3.org/TR/2004/WD-voicexml21-20040728/) for examples and detailed usage information for this tag.

### Example

```markup
<?xml version="1.0"?>
<vxml version="2.1">
  <form>
    <script>
      var movies = new Array();
      movies[0] = new Object();
      movies[0].audio = 'godfather.wav';
      movies[0].tts = 'The Godfather';
      movies[1] = new Object();
      movies[1].audio = 'highfidelity.wav';
      movies[1].tts = 'High Fidelity';
      movies[2] = new Object();
      movies[2].audio = 'raiders.wav';
      movies[2].tts = 'Raiders of The Lost Ark';
    </script>
    <field name="mov">
      <prompt>
        Please select one of the following movies.
        <break time="500ms"/>
        <foreach item="movie" array="movies">
          <audio expr="movie.audio"><value expr="movie.tts"/></audio>
          <break time="500ms"/>
        </foreach>
      </prompt>
      <grammar mode="voice" type="application/srgs+xml" root="ROOT">
        <rule id="ROOT">
          <one-of>
            <item>The Godfather</item>
            <item>High Fidelity</item>
            <item>Raiders of The Lost Ark</item>
          </one-of>
        </rule>
      </grammar>
      <filled>
        You said <value expr="mov"/>.
      </filled>
    </field>
  </form>
</vxml>
```

The output of the above script would be:

> Computer: Please select one of the following movies.\
> Computer: The Godfather, High Fidelity, Raiders of The Lost Ark.\
> Human: High Fidelity.\
> Computer: You said High Fidelity.

### Child Tags

[\<audio>](https://docs.plumvoice.com/dev/voicexml/tags/audio-tag), [\<assign>](https://docs.plumvoice.com/dev/voicexml/tags/assign-tag), [\<break>](https://docs.plumvoice.com/dev/voicexml/tags/break-tag), [\<clear>](https://docs.plumvoice.com/dev/voicexml/tags/clear-tag), [\<data>](https://docs.plumvoice.com/dev/voicexml/tags/data-tag), [\<disconnect>](https://docs.plumvoice.com/dev/voicexml/tags/disconnect-tag), [\<emphasis>](https://docs.plumvoice.com/dev/voicexml/tags/emphasis-tag), [\<enumerate>](https://docs.plumvoice.com/dev/voicexml/tags/enumerate-tag), [\<exit>](https://docs.plumvoice.com/dev/voicexml/tags/exit-tag), [\<foreach>](https://docs.plumvoice.com/dev/voicexml/tags/foreach-tag), [\<goto>](https://docs.plumvoice.com/dev/voicexml/tags/goto-tag), [\<if>](https://docs.plumvoice.com/dev/voicexml/tags/if-tag), [\<log>](https://docs.plumvoice.com/dev/voicexml/tags/log-tag), [\<metadata>](https://docs.plumvoice.com/dev/voicexml/tags/metadata-tag), [\<mark>](https://docs.plumvoice.com/dev/voicexml/tags/mark-tag), [\<paragraph>](https://docs.plumvoice.com/dev/voicexml/tags/paragraph-tag), [\<phoneme>](https://docs.plumvoice.com/dev/voicexml/tags/phoneme-tag), [\<prompt>](https://docs.plumvoice.com/dev/voicexml/tags/prompt-tag), [\<prosody>](https://docs.plumvoice.com/dev/voicexml/tags/prosody-tag), [\<reprompt>](https://docs.plumvoice.com/dev/voicexml/tags/reprompt-tag), [\<return>](https://docs.plumvoice.com/dev/voicexml/tags/return-tag), [\<say-as>](https://docs.plumvoice.com/dev/voicexml/tags/say-as-tag), [\<sentence>](https://docs.plumvoice.com/dev/voicexml/tags/sentence-tag), [\<script>](https://docs.plumvoice.com/dev/voicexml/tags/script-tag), [\<speak>](https://docs.plumvoice.com/dev/voicexml/tags/speak-tag), [\<sub>](https://docs.plumvoice.com/dev/voicexml/tags/sub-tag), [\<submit>](https://docs.plumvoice.com/dev/voicexml/tags/submit-tag), [\<throw>](https://docs.plumvoice.com/dev/voicexml/tags/throw-tag), [\<value>](https://docs.plumvoice.com/dev/voicexml/tags/value-tag), [\<var>](https://docs.plumvoice.com/dev/voicexml/tags/var-tag), [\<voice>](https://docs.plumvoice.com/dev/voicexml/tags/voice-tag)

### Parent Tags

[\<block>](https://docs.plumvoice.com/dev/voicexml/tags/block-tag), [\<catch>](https://docs.plumvoice.com/dev/voicexml/tags/catch-tag), [\<error>](https://docs.plumvoice.com/dev/voicexml/tags/error-tag), [\<filled>](https://docs.plumvoice.com/dev/voicexml/tags/filled-tag), [\<foreach>](https://docs.plumvoice.com/dev/voicexml/tags/foreach-tag), [\<help>](https://docs.plumvoice.com/dev/voicexml/tags/help-tag), [\<if>](https://docs.plumvoice.com/dev/voicexml/tags/if-tag), [\<noinput>](https://docs.plumvoice.com/dev/voicexml/tags/noinput-tag), [\<nomatch>](https://docs.plumvoice.com/dev/voicexml/tags/nomatch-tag), [\<prompt>](https://docs.plumvoice.com/dev/voicexml/tags/prompt-tag)
