1

imagine i have a div

<div id="blah">
spotify:track:something
spotify:track:something
spotify:track:something
spotify:track:something
</div>

With close to 100+ of spotify:track stuff and I want to drag whole list to Spotify program to create a new playlist, but first I would have to select all the text.

Is it possible to make that starting to drag div automatically would select whole text.

For example: http://codepen.io/anon/pen/Hwbdy

It actually doesn't if it's a div or text area as long as a user can simple click and drag without selecting the text, thanks

Karolis Mazukna
  • 377
  • 1
  • 6
  • 12

2 Answers2

1

if you want to select texts by javascript try this:

function select(){
    var div = document.getElementById('blah'),
        sel, range;
    if(window.getSelection){
        range = document.createRange();
        range.selectNode(div)
        sel = window.getSelection();
        sel.addRange(range);
    }else{
        range = document.body.createTextRange();
        range.moveToElementText(div);
        range.select();
    }
}

demo / jsfiddle

frogatto
  • 28,539
  • 11
  • 83
  • 129
0

Maybe use jQuery? :

$("#yourID").focus(function() {
var $this = $(this);
$this.select();

// Work around Chrome's little problem
$this.mouseup(function() {
    // Prevent further mouseup intervention
    $this.unbind("mouseup");
    return false;
});
});
Fred
  • 2,402
  • 4
  • 31
  • 58