0

I'm using the following code to edit a text in contenteditable div by changing its font. when clicking on a div which is a stylized dropdown menu the selection disappears in chrome. It works fine in firefox, opera and even in IE!

Rangy doesn't find any selected text to deal with. I'm wondering why!

Note: it works fine when using 'mouseover' instead of 'click'

DropDown.prototype = {
    initEvents : function() {
        var obj = this;

        obj.dt.on('click', function(event){
            $(this).toggleClass('active');
            if (savedSel) {
                rangy.removeMarkers(savedSel);
            }
            savedSel = rangy.saveSelection();
            savedSelActiveElement = document.activeElement;
            return false;
        });

    }
}
Tim Down
  • 318,141
  • 75
  • 454
  • 536
Develop Smith
  • 146
  • 1
  • 3
  • 13

2 Answers2

3

One workaround is to use the mousedown event rather than the click event and preventing the default behaviour.

obj.dt.mousedown(function(event){
    event.preventDefault();
    $(this).toggleClass('active');
    if (savedSel) {
        rangy.removeMarkers(savedSel);
    }
    savedSel = rangy.saveSelection();
    savedSelActiveElement = document.activeElement;
    return false;
});

Demo: http://jsfiddle.net/NJuMK/1/

Tim Down
  • 318,141
  • 75
  • 454
  • 536
0

You should save the selection data before the click happens. For example, save it on the mouse over, and use it when the user clicks.

Jodes
  • 14,118
  • 26
  • 97
  • 156