First bindobsolete in the new versions of jQuery, instead use on.
Secondly, you can save the event parentin mousedownand check it in mouseover. This way you can check if it is tdin the same tror not. Updated code might look like this:
var isMouseDown = false, parent;
$("#mytable td").mousedown(function () {
var $this = $(this);
isMouseDown = true;
parent = $this.closest('tr').get(0);
$this.toggleClass("highlighted");
return false;
}).mouseover(function () {
var $this = $(this);
if (isMouseDown && parent === $this.closest('tr').get(0)) {
$this.toggleClass("highlighted");
}
}).on("selectstart", function () {
return false;
});
Jsfiddle
Update:
. , mouseenter ( mouseover) mousedown. , .
JSFiddle:
var isMouseDown = false,
$cells = $('#mytable td');
$cells.on('mousedown', function() {
$cells.removeClass('highlighted');
isMouseDown = true;
$(this)
.toggleClass('highlighted')
.siblings('td')
.on('mouseenter', onMouseEnter);
return false;
}).on('mouseup', function() {
$(this).siblings('td').off('mouseenter');
}).on('selectstart', function() {
return false;
});
function onMouseEnter() {
if (isMouseDown) {
$(this).toggleClass("highlighted");
}
}