Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden text that can be dragged from the browser?

How can you create an html element that when dragged from the browser into a text editor, hidden text on or in the dragged element will be pasted into the editor?

My first thought was to use the href attribute on the anchor tag:

<a href="hidden message text here">Drag me into a text editor!</a>

This works great in chrome, but firefox and safari remove spaces from the href value which renders the copied message unusable.

Any ideas?

like image 677
Zaqx Avatar asked Nov 28 '12 01:11

Zaqx


People also ask

How do I see hidden text on a website?

You could use a variety of methods to find hidden text and links. Some of the fastest are hitting 'ctrl-a' (select all) to see if any text or links light up that were hidden before.

What is hidden text in HTML?

Definition and Usage. The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

How do you make text not visible in HTML?

To hide text from a screen reader and display it visually, use the aria-hidden attribute and set it to true. To hide text from a screen reader and hide it visually use the hidden attribute. You can also use CSS to set display: none or visibility: hidden to hide an element from screen readers and visually.

What HTML elements are draggable?

It's worth noting that in most browsers, text selections, images, and links are draggable by default. For example, if you drag a link on a web page you will see a small box with a title and URL. You can drop this box on the address bar or the desktop to create a shortcut or to navigate to the link.


2 Answers

Instead of manipulating the browser's default behavior for dragging text/links/images, you want to set the data to something arbitrary in the dragstart event.

For example, use the text from a hidden #content:

$('[draggable]').on('dragstart', function(e) {
    var content = $(this).find('#content').text(); // Can be anything you want!
    e.originalEvent.dataTransfer.setData('text/plain', content);
    $(this).addClass('dragging');
});

Here is a working JSFiddle

like image 87
Anson Avatar answered Oct 18 '22 10:10

Anson


For versions of IE below 10 which don't support the dataTransfer method, I've discovered another somewhat hacky way to make this work.

Basically you make text invisible with css then use js to select it in the background on hover.

HTML

<div id="drag_area" draggable="true">
    <div id="text">
      hidden text
    </div>
</div>​

CSS

#text { filter:alpha(opacity=0); opacity:0;
        overflow:hidden; z-index:100000; width:180px; height:50px }

​ JS

    function selectText(elementID) {
        var text = document.getElementById(elementID);
        if ($.browser.msie) {
            var range = document.body.createTextRange();
            range.moveToElementText(text);
            range.select();        
        } else if ($.browser.mozilla || $.browser.opera) {
            var selection = window.getSelection();
            var range = document.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range);
        } else {
            var selection = window.getSelection();
            selection.setBaseAndExtent(text, 0, text, 1);
        }
    }
    $('#drag_area').hover(function(){
        selectText('text');  
    });

Here it is combined with Anson's solution and a little feature detection: http://jsfiddle.net/zaqx/PB6XL/ (works in IE8, IE9 and all modern browsers)

EDIT: Updated Fiddle in the comments below.

like image 42
Zaqx Avatar answered Oct 18 '22 11:10

Zaqx