Solved

Simulate click in link with a key stroke...

Posted on 2006-11-28
3
514 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 Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

821 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