Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simulate click in link with a key stroke...

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

609 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