0

I am a newbie in Javascript Speech Recognition and chose to develop using Annyang library. I want to trigger the "show date" button (without clicking the button) when the user says 'hello'. However, it didn't work. Below is my source codes. Need some help and thank you.

<script src="https://cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js"></script>
<button type="button" id="subject" onclick="getElementById('demo').innerHTML=Date()">Show 
    Date</button>
<p id="demo"></p>
<script>
  window.onload = function() {
    if (annyang) {
      var commands = {
        'Hello': function() {
          document.querySelectorAll('#subject').click();
        }
      };
      annyang.addCommands(commands);
      annyang.start();
    }
  }
</script>
Vivek Jain
  • 2,730
  • 6
  • 12
  • 27
zhitingzt
  • 1
  • 3

1 Answers1

0

Try replacing document.querySelectorAll with document.getElementById.

querySelectorAll returns an array of elements, not just a single element (which is what getElementById does). So you are actually calling click() on an array and not on the button. You can't "click" an array.

There may be other problems in the code as well, I haven't run it myself, but that issue jumped at me.

Tal Ater
  • 1,121
  • 1
  • 10
  • 17