I am developing a chrome extension. I want to show a popup when long pressed on the current open page or tab. How do I detect when a text on the page is long pressed?
Asked
Active
Viewed 514 times
0
-
1There's an answer with a working demo here: https://stackoverflow.com/questions/19539329/how-to-apply-long-click-event-and-doubleclick-event-on-the-same-element-in-javas – Walk Oct 03 '17 at 12:02
1 Answers
0
as Buley describes in this post: How to detect a long press on a div in Jquery?, you have to watch both mouseup and mousedown events and calculate the difference.
(function() {
// how many milliseconds is a long press?
var longpress = 3000;
// holds the start time
var start;
jQuery( "#pressme" ).on( 'mousedown', function( e ) {
start = new Date().getTime();
} );
jQuery( "#pressme" ).on( 'mouseleave', function( e ) {
start = 0;
} );
jQuery( "#pressme" ).on( 'mouseup', function( e ) {
if ( new Date().getTime() >= ( start + longpress ) ) {
// If you want to get the text of the element, use jQuery's element.text()
alert('long press! You pressed: ' + $(this).text());
} else {
alert('long press! You pressed: ' + $(this).text());
}
} );
}());
Although this is a jQuery example, this is also possible without(see Walk's example)

Mr. Greenwoodz
- 255
- 1
- 11
-
Thanks. This is what I was looking for. but there is a slight difference. I want to get the text on which mouse long pressed. It might be any text on the page. – ilovecse Oct 03 '17 at 12:17
-
If i understand correctly you want to return the value of the text on which you click? – Mr. Greenwoodz Oct 03 '17 at 12:21
-
-
I've edited the example. Clicking the button should now return the text of the element. This is an example with a button but you can bind this to any element you want;) – Mr. Greenwoodz Oct 03 '17 at 12:38
-
I checked the code. There is a tiny problem. I just want the text only like if a paragraph tag has "This is me" value in it and when i click for more than 1 sec on "me" , i need the text "me " not the whole "This is me " sentence. just the word. Thanks for the help. – ilovecse Oct 03 '17 at 12:48
-
A quick google search and i think i've found exactly what you need. Check this link: https://stackoverflow.com/questions/7563169/detect-which-word-has-been-clicked-on-within-a-text – Mr. Greenwoodz Oct 03 '17 at 12:52
-
yeah tried the code.... it works thanks. There is an error when pressed on "About 19,30,000 results (0.40 seconds) " on google search page. when clicked on "about" Uncaught DOMException: Failed to execute 'setStart' on 'Range': The offset 4294967295 is larger than the node's length (23). – ilovecse Oct 03 '17 at 13:04
-
It's hard to tell what you are doing wrong without an example. The link i posted also has other examples in it. You could try those. If it's still not working, please post an example with code to clearify your problem. – Mr. Greenwoodz Oct 03 '17 at 13:25
-
-
Hi I got a weird behavior.. dunno what's wrong. I viewed your good in jsfiddle ang it works like charm... but when I try to use your code on my mouseup event is not firing. Its like lost.. something like that. – Edang Jeorlie May 31 '18 at 03:29