Jump to content

Help with example off jsfiddle.net


DataSpy

Recommended Posts

Problem:

I want to convert the enter key to a tab key, so when a user hits enter it acts like tab.

 

Solution:

I found the javascript to fix my problem but it's not work, I have very little experience with javascript :(

 

Where I found the code: http://stackoverflow.com/questions/4494552/change-enter-from-submission-to-tab

 

Demo of the code online: http://jsfiddle.net/GRtQY/19/

 

I know it uses Jquery and I do have that included in my script, I'm including the javascript in the head of my web page, I'm wondering if that's the problem.  Right above the framework option on http://jsfiddle.net/GRtQY/19/ there's an option (I'm assuming) of how to include the script, if I use "no wrap (head)" the script won't work but if I use "onLoad" it does.

 

On my webpage:

 

(in between head tags)

$("input").bind("keydown", function(event) {
    if (event.which === 13) {
        event.stopPropagation();
        event.preventDefault();
        $(this).nextAll("input").eq(0).focus();
    }
});

 

 

(in between body tags)

<form action="#">
    <input name="field-one"/>
    <div>asdadsa</div>
    <input name="field-two"/>
    <input type="submit" value="submit"/>
</form>

 

Any help greatly appreciated, Thanks in advance!!!

Link to comment
Share on other sites

This works :)

 

$(document).ready(function() {
var focusables = $(":input").not('[type="image"]').not('[type="submit"]');

  focusables.keydown(function(e) {
    if (e.keyCode == 13) {
      e.preventDefault();
      var current = focusables.index(this),
              next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
      next.focus();
    }
  });
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.