Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you click text that's inside a hyperlink so you can drag and highlight it?

If you have a paragraph of normal text, you can click anywhere inside the text and drag your mouse to highlight it. However, if the text is inside an anchor/link tag, it won't allow you to click and drag -- the link catches the mouse and prevents it from happening.

To see what I mean, find a text link on this page and try to click inside it and drag to select it. It won't let you -- you'll have to click outside the link and drag around it.

What do you need to do in JavaScript/JQuery to temporarily disable the link long enough for you to drag and highlight it?

The reason I can't just start outside the link is because every word in the paragraph is within a hyperlink -- it's a video transcript and each link is synced to a segment in the video.

like image 414
espeed Avatar asked Nov 04 '22 16:11

espeed


1 Answers

My first thought was to do something like this:

$("a").each(function() {
   $(this).replaceWith("<span class='link' data-href='" + $(this).attr("href") + "'>" + $(this).text() + "</span>"); 
});
$(".link").click(function() {
   window.location = $(this).data("href"); 
});

However, there may be a much better way of doing this. The code above converts all a elements into span elements, keeping the href attribute value, and then makes the span elements with class "link" function as links. You could style the span elements to look like a elements currently do on your page.

Here's an example of the above in action.

like image 175
James Allardice Avatar answered Nov 14 '22 03:11

James Allardice