2

I am create a form in html for submit data. When I shall reach save button by tab press and press tab again then tab will act as a Enter key. That means data will save. How can i do this?

I have use the following code

 <form id="form">
<input type="text" />
<input type="text" />
<input type="text" />
<input disabled="disabled" />
<input readonly="readonly" value="readonly" />
<textarea></textarea>

<input type="submit" />

$(":input").on("keydown", function(event) {
    if (event.which == 9) {
        event.startPropagation();
    }
});

Please tell me the solution.

VisioN
  • 143,310
  • 32
  • 282
  • 281
Bir
  • 794
  • 1
  • 15
  • 31

2 Answers2

2

an alternative to mjalajel 's answer

$("input[type='submit']").on("keyup", function(event) {
    if (event.which == 9) {
        $(this).trigger('click');
    }
});

this way whatever you have on click event is called

if you want to follow a link, you have to replace

$(this).trigger('click');

with

this.click();

this is because jquery click event isn't the same as a "real" click more info here: jQuery: how to trigger anchor link's click event

Community
  • 1
  • 1
Jarry
  • 1,891
  • 3
  • 15
  • 27
  • Thanks. You code is working. I am facing another problem. How can i go a new link using tab. Here is my code http://jsfiddle.net/alamin5433/T93N7/ – Bir Dec 23 '13 at 15:50
  • edited with a possible way to do that, tough you have to check if this is crossbrowser – Jarry Dec 23 '13 at 16:09
  • Jarry, Code is not working for safari and firefox 16 – Bir Dec 23 '13 at 17:31
1

I assume this is what you're trying to achieve

$("input[type='submit']").on("keyup", function(event) {
    if (event.which == 9) {
        $('#form').submit();
    }
});
mjalajel
  • 2,171
  • 21
  • 27
  • Thanks it works. I have another problem. How can i go the next page using tab. just next button instead of save. this is the next button code – Bir Dec 23 '13 at 15:22
  • I have tried Jarry's code. But not working. Please check here http://jsfiddle.net/alamin5433/T93N7/ – Bir Dec 23 '13 at 15:41