# Play random sound field only

**URL:** <https://forums.ankiweb.net/t/play-random-sound-field-only/40091>\
**Category:** Card Design\
**Created:** [January 22, 2024, 12:40am UTC](https://forums.ankiweb.net/t/play-random-sound-field-only/40091 "2024-01-22T00:40:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonsofMoons](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@MoonsofMoons](https://forums.ankiweb.net/u/MoonsofMoons)\
**Post date:** [January 22, 2024, 12:40am UTC](https://forums.ankiweb.net/t/play-random-sound-field-only/40091/1 "2024-01-22T00:40:48Z")

</div>

Hello,

I have a script that works fine for image fields, but when used to call out sound file fields, all the sounds play in order, rather than only the randomly selected one. Does anyone know a way to correct this?

```auto
<div class="snd" hidden>{{Sound1}}</div>
<div class="snd" hidden>{{Sound2}}</div>
<div class="snd" hidden>{{Sound3}}</div>

<script>

	{
  const fields = document.querySelectorAll(".snd");

	function randomInt(max) {
		return Math.floor(Math.random() * max);
	}		
	rand = randomInt(fields.length)

	fields[rand].hidden = false;
  }
</script>

```

---

<div class="post-metadata">

**Author:** ![jcznk](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/jcznk/32/15215_2.png) [@jcznk](https://forums.ankiweb.net/u/jcznk)\
**Post date:** [January 22, 2024, 4:51am UTC](https://forums.ankiweb.net/t/play-random-sound-field-only/40091/2 "2024-01-22T04:51:07Z")

</div>

I think the root of the problem is that your approach simply hides all but one random audio buttons, however, Anki will automatically play all the sounds even if the replay buttons are hidden.

The following script seems to work fine on my computer. It tries to work around the issue by using JS to ‘click’ on the randomly selected replay button, which in turn should prevent the automatic reproduction of all other audio files:

```auto
<div class="snd" hidden>{{Sound1}}</div>
<div class="snd" hidden>{{Sound2}}</div>
<div class="snd" hidden>{{Sound3}}</div>

<script>
	{
  const fields = document.querySelectorAll(".snd");
  const soundButtons = document.querySelectorAll(".snd .replay-button");

	function randomInt(max) {
		return Math.floor(Math.random() * max);
	}		
	rand = randomInt(fields.length)
  
    fields[rand].hidden = false;
    soundButtons[rand].click();
  }
</script>

```

---

<div class="post-metadata">

**Author:** ![MoonsofMoons](https://avatars.discourse-cdn.com/v4/letter/m/6bbea6/32.png) [@MoonsofMoons](https://forums.ankiweb.net/u/MoonsofMoons)\
**Post date:** [January 22, 2024, 4:56am UTC](https://forums.ankiweb.net/t/play-random-sound-field-only/40091/3 "2024-01-22T04:56:31Z")

</div>

Yup. That works on my end also. Big Thanks!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [February 21, 2024, 4:57am UTC](https://forums.ankiweb.net/t/play-random-sound-field-only/40091/4 "2024-02-21T04:57:14Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
