Solved

Simulate click in link with a key stroke...

Posted on 2006-11-28
3
511 Views
Last Modified: 2012-05-05
Hello World!

I have a page that contains a dynamic anchor link and image and I would like the user to be able to press enter to automatically follow that link.  I was able to do this but I'm not sure if this is the right solution. I guess my question is ... Is this the only or best way to simulate a click with a key stroke?

Here is what I have so far:

<HTML>
<HEAD>

<SCRIPT TYPE="text/javascript">
      function DoKeyPress()
      {
            if (event.keyCode== 13)
            {
                  //Submit Anchor link...
                  var elem = document.getElementById('mylink');
                  window.location = elem;                  
            }
      }
</SCRIPT>
</HEAD>
<BODY onload="document.onkeypress=DoKeyPress;">
      <H1>This is a test</H1>
      <A id=mylink href="http://www.yahoo.com"><IMG src="mylinkimage.gif"></A><BR>
</BODY>
</HTML>



-- Bubba
0
Comment
Question by:bganoush
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
system_down earned 125 total points
ID: 18029196
hi

try this, your's doesn't work in firefox2

<HTML>
<HEAD>

<SCRIPT TYPE="text/javascript">

        function MapKeyDown(loEvent) {
        loEvent = loEvent||event;
        var nKeyCode = loEvent.which||loEvent.keyCode;
        var target = loEvent.srcElement||loEvent.target;
        //alert('MapKeyDown(): nKeyCode = ' + String(nKeyCode) + ', target: ' + target.tagName);
          
            if (nKeyCode == 13)
          {
               //Submit Anchor link...
               var elem = document.getElementById('mylink');
               window.location = elem;              
          }
            }
          
       function init() {
        document.onkeydown = MapKeyDown;
     }      
      
</SCRIPT>
</HEAD>
<BODY onload="init()">
     <H1>This is a test</H1>
     <A id=mylink href="http://www.yahoo.com"><IMG src="mylinkimage.gif"></A><BR>
</BODY>
</HTML>

Let me know
S_D
0
 

Author Comment

by:bganoush
ID: 18029347

Thanks,

We don't support Firefox here but it's good to have a complete solution.

-- Bubba
0
 
LVL 6

Expert Comment

by:system_down
ID: 18029383

Your welcome.
S_D
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now