Bit of a confusing one, trying to add keyboard shortcuts for some urls on my page using https://craig.is/killing/mice mousetrap.
I have this to set the keys:
Mousetrap.bind('up', function() { document.getElementById("n").click(); });
Mousetrap.bind('down', function() { document.getElementById("s").click(); });
Mousetrap.bind('left', function() { document.getElementById("w").click(); });
Mousetrap.bind('right', function() { document.getElementById("e").click(); });
Mousetrap.bind('e', function() { document.getElementById("gatherEnergy").click(); });
Then the html for the arrow keys which works fine:
<area id="n" alt="North" title="North" href="#" onclick="moven();refreshIframe();" shape="poly" coords="49,1,50,45,113,45,110,1" />
<area id="e" alt="East" title="East" href="#" onclick="movee();refreshIframe();" shape="poly" coords="118,52,156,53,156,113,118,115" />
<area id="w" alt="West" title="West" href="#" onclick="movew();refreshIframe();" shape="poly" coords="42,52,0,51,1,115,40,117" />
<area id="s" alt="South" title="South" href="#" onclick="moves();refreshIframe();" shape="poly" coords="49,126,51,164,114,165,112,127" />
Which all works fine and does as intended the urls are clicked on pressing the arrow keys, however when I get to the key "e" I'm using a href rather than an area:
<a href="map.php?act=e" id="gatherEnergy" onclick="refreshIframe();">Gather energy</a>
This returns the error: Uncaught TypeError: Cannot read property 'click' of null
Does document.getElementById(#ID).click(); not work on a hrefs for some reason? Or am I well off the mark? Really confused with this one so any advice would be great!
EDIT: Setup a jsfiddle with it https://jsfiddle.net/Lek3ptvp/ and that works!
On my localhost the url I'm trying to trigger is in a iframe, would this be causing the issue? How do I make the .document.getElementById look in the iframe?