?
Solved

Simulate click in link with a key stroke...

Posted on 2006-11-28
3
Medium Priority
?
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
system_down earned 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

764 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