I've got the following tablesorter widget, which makes the td
elements into contentEditable divs when clicked, and then converts them back into td
s when the user clicks outside the cell.
$simjq.tablesorter.addWidget({
id: 'makeColumnsEditable',
format:function(table){
var searchString = '',
targetColumns = table.config.widgetOptions
.makeColumnsEditable.targetColumns,
inClick = false,
thisCell = $simjq(),
thisCellHtml = null;
while (targetColumns.length){
searchString.length ?
searchString += (',td:eq('+targetColumns.pop()+')') :
searchString += ('td:eq('+targetColumns.pop()+')');
}
function exitSelection(){
//Re-attach text to cell and get rid of the div
thisCellHtml = thisCell.children('div').html();
thisCell.empty();
thisCell.html(thisCellHtml);
//Exit current selection
thisCell = $simjq();
//unbind handler from document
$simjq(document).unbind('click', exitSelection)
}
$simjq(table).find('tr').each(function(){
$simjq(this).find(searchString).on('click', function(event){
if(!inClick || (thisCell[0] == $simjq(this)[0])){
event.stopPropagation();
if (thisCell[0] != $simjq(this)[0]){
inClick = true;
thisCell = $simjq(this);
thisCellHtml = thisCell.html();
thisCell.empty();
thisCell.append('<div contentEditable="true">'
+thisCellHtml + '</div>');
$simjq(document).bind('click', exitSelection);
}
} else {
//Allow click to bubble up to document
inClick = false;
};
});
});
}
});
My issue is that, when a click turns a div
into a contentEditable div, it doesn't select them, so it takes an extra click to enter the target element and edit it. How to I enter the editable div (i.e. place the carat in the text) when I click on the div, rather than requiring an extra click?