Kerenlu
Kerenlu

Reputation: 65

get selected text with taphold event

I want to mark the text I have selected using the taphold event. See the following code:

$(function () {

    $('body').bind('taphold', function (e) {
        $(this).addClass('highlighted');
        e.stopImmediatePropagation();
        return false;
    });

});

But $(this) return the body, how can I get the selected content?

Upvotes: 1

Views: 431

Answers (2)

user123456
user123456

Reputation: 2659

$(document).delegate('Here it should be the text selector like id or class name ', 'taphold', function (e) {

             $(this).addClass('highlighted');
             e.stopImmediatePropagation();
             return false;
        });

Hope this is helpful.

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

As you bind the event with the body so $(this) will return the body, but e.target will return your target element on which you fire you event.

$(function () {
    $('body').bind('taphold', function (e) {
        $(e.target).addClass('highlighted');   // use e.target instead of this
        e.stopImmediatePropagation();
        return false;
    });
});

Upvotes: 1

Related Questions