Tomatrox
Tomatrox

Reputation: 693

HTML Tooltip after selecting text in an <input> or in a <textarea>

Ok, so basically what I need is that when I select some text in an <input> or in a <textarea>, a tooltip appears with two buttons, and then get the selected text in a Javascript variable.

The image below is an example of what I need:

var selectedText = "s is an in"

I'm using Powertip for the tooltip and Rangy for manipulating selections in <input> and <textarea>, the problem is that I can't manage to popup the tooltip and get the selected text in a JS variable after I select the text with the cursor.

Any suggestions on how to achieve this?

Thanks in advance.

UPDATE:

Thanks to Todd answer, I'm sharing what I was trying to achieve: JSFiddle

Upvotes: 1

Views: 3650

Answers (3)

Sean Carroll
Sean Carroll

Reputation: 107

Use a Modal to create popup, create a Text Field Select Event & a Function to trigger open anchor. Thats all you need.

here is a working example I have created:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Text Select Modal</title>

<style>
    .modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}
.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}

.modalDialog > div {
    width: 300px;
    height:130px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #151414;

}
.close {
    background: #606061;
    color: #FFFFFF;
    line-height: 25px;
    position: absolute;
    right: -12px;
    text-align: center;
    top: -10px;
    width: 24px;
    text-decoration: none;
    font-weight: bold;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    border-radius: 12px;
    -moz-box-shadow: 1px 1px 3px #000;
    -webkit-box-shadow: 1px 1px 3px #000;
    box-shadow: 1px 1px 3px #000;
}

.close:hover { background: #00d9ff; }
</style>
<script type="text/javascript">
function myFunction() {
    window.open("#openModal","_self");
}
</script>
</head>

<body>
<input type="text" onSelect="myFunction()">
<div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>
        <br>
        <br>
        <br>
        <br>
        <br>
        <input type="button" value="Button 1">
        <input type="button" value="Button 2">
    </div>
</div>
</body>
</html>

Upvotes: 0

Todd
Todd

Reputation: 5454

This seems to work

$('input, textarea').powerTip({ manual: true }); // have to prevent default plugin


$('input, textarea').on({
    'select': function() {
        var selectedText = window.getSelection().toString();
        $(this).powertip('show');
     },
     'blur': function() {
        $.powerTip.hide();
     }
});

Upvotes: 2

Varun Chakervarti
Varun Chakervarti

Reputation: 1042

You can add mouse up event on selection of a text and can get the selected text, after that when you will show the selected text, you can also show the tool-tip with that.

Here is the JSFiddle Link for showing the text selection, you can update this and can also show the tool-tip where I have printed the selected text.

How to show tool-tip box:

Step-1) You need to get the top and left of the input field.

Step-2) Create a box using HTML and CSS.

Step-3) Set it's position to absolute and it's container to relative.

Step-4) Set it's top and left to that you have received in Step-1.

Upvotes: 0

Related Questions