I have a JQuery-based single-page web application with a rich Knockout-based grid.
When I double-click on a row in the Knockout grid, I load a virtual page of data with the details for the selected item, and when I’m done editing it (or if I cancel), I’m returned to the grid. All of this happens on a single page.
If I double-click on a non-text area in the grid or if I set focus to an input field in the form, everything works great when the grid is redisplayed. But if I double-click on some text in a row (like the row item’s “Name”) and then immediately click “Cancel” to return to the grid, then all of grid’s HTML is selected when I redisplay it. It’s as if I had clicked and dragged through the grid to select a giant region of text.
I’ve been trying to programmatically deselect this selected text but to no avail. I’ve tried various combinations of:
$(document).focus();
$("#idBody").focus();
$("#idBody").trigger("click");
If I click on a region outside the grid, I get the behavior I want. What are my options?
The problem when double clicking is that by performing that action browsers by default select the text under the cursor, creating a range of selected text.
Working Fiddle Example!
This solved me the same problem in the past by removing that range of selected text:
You can read about it:
window.getSelection at MDN and DOM Selection at MDN
removeAllRanges method at MSDN and HTMLSelection object at MSDN
Credit for this solution goes to this answer and Mr. Y..
TESTED ON
Windows XP Profissional versão 2002 Service Pack 3
Windows 7 Home Edition Service Pack 1
Linux Ubuntu 12.04