Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simulate click in link with a key stroke...

Posted on 2006-11-28
3
Medium Priority
?
523 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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

810 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