1

I have a script that assigns tabindex to visible elements on a page. Most of those are form elements but I also needed to add a way to add a div that I use to show/hide a section.

<span class="tabInto">Show/Hide</span>

I do this with jQuery

$(':input:visible, .tabInto').each(function (i) {
    $(this).attr('tabindex', i + 1);
});

So, when I tab through the form fields I am able to tab into that span as well. Now I need to be able to simulate click action, to expand hidden section, by using keyboard when I am focused on that span. How Can I do that? Is it usually done with Enter or Space bar?..

3
  • 1
    $("selector").trigger("click")? Commented Aug 27, 2013 at 20:58
  • I'm not sure but you could call $("filter element").click();. Commented Aug 27, 2013 at 20:58
  • If you use an anchor instead of a span I believe this is default browser functionality. Commented Aug 27, 2013 at 21:00

2 Answers 2

1

If you are using an anchor tag (with an href), then this is default functionality. Hitting the enter key while the element has focus will trigger the click handler.

You can simulate this same functionality with a span in the following way:

$("span").click(function () {
    alert("click span");
});

$("span").keypress(function (e) {
    if (e.keyCode == 13) {
        $(this).click();
    }
});

http://jsfiddle.net/dLmyV/

Sign up to request clarification or add additional context in comments.

Comments

0

Using $(".tabInto").trigger("click")

This way you fire the 'click' event on the tabInto class.

Source: http://api.jquery.com/trigger/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.