Chrome selects all text content on focus

by dhchow

HTML

<div contenteditable="true" id="text" style="height:300px; width: 300px;">
    some
    <span id="bookmark" class="test">&nbsp;</span>
    text
</div>
<button id="remove">Remove bookmark</button>

JavaScript

$(function() {
    var element = $("#text")[0];
    var bookmark = $("#bookmark", $("#text"))[0];

    if ($("#bookmark", $("#text")).attr("class") != "test") alert("bookmark is all wrong");

    $("#remove").click(function() {
        //alert(bookmark.createControlRange)
        if (element.createControlRange) {
            var controlRange = element.createControlRange();
            alert(controlRange.length)
            controlRange.add(bookmark)
            alert(controlRange.length)
            element.select();
            document.selection.clear();
        }
    });
})