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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.