el Dude
el Dude

Reputation: 5183

Opera bug with JS autoselecting text (if more than 1 div)

Here is HTML code. It supposed to select all text in "Container" div

<B onclick="SelectText(document.getElementById('Container'));">select all text</B>
<Div id="Container">
<Div>123456</Div>
<Div>123456</Div>
<Div onclick="SelectText();">123456</Div>
</Div>

here is JS code of the SelectText() function

function SelectText(target){
if(target==null){
var e = window.event || e;
if (!e) var e = window.event;
var target=e.target || e.srcElement;
}

var rng, sel;
if ( document.createRange ) {
rng = document.createRange();
rng.selectNode( target );
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange( rng );
} else {
var rng = document.body.createTextRange();
rng.moveToElementText( target );
rng.select();
    }
}

Problem is that in Opera 12.02 when "select all text" is clicked, all text seems like selected, but it's not selected (I can't rightclick it and copy).

(terrific, but IE works fine with it)

Why not in Opera?!!! And what can I do to make Opera 12.02 believe that all text in "Container" is selected?

Upvotes: 0

Views: 77

Answers (1)

hallvors
hallvors

Reputation: 6229

That code works fine for me in 12.11 on jsfiddle, so I guess you just need to use a more recent Opera version. I remember older Opera versions having bugs like that.

Upvotes: 1

Related Questions