David Y. Stephenson
David Y. Stephenson

Reputation: 980

Using jQuery Hotkeys to Select Text Area without writing text

I am using jeresig's jQuery Hotkeys library to enable hotkeys that can select a text area on a webpage. However, when I use the hotkey, it inserts text into the text area instead of only selecting it, overwriting the previous content, which is not what I want. Is there a solution to this?

HTML:

<input id='test' type='text' value='content'> <span class='button'>Button</span>

JavaScript:

$(document).ready(function () {
    // Clicking button selects associated text area
    $('.button').click(function () {
        $(this).siblings('input:text').focus();
    });

    // Hotkeys for text area
    $(document).bind('keypress', 'a', function () {
        $('#test').focus()
    });
});

jsfiddle

Upvotes: 0

Views: 370

Answers (1)

Iqbal Fauzi
Iqbal Fauzi

Reputation: 1571

Don't use keypress event, it can be executed many times. Use keyup instead.

Upvotes: 2

Related Questions