Menaka Kariyawasam Menaka Kariyawasam - 2 months ago 17
jQuery Question

How to check checkbox when right click in jQuery

This is my working table

There are checkboxes in table. I want to check those row by row when right click on the row.

It's working with click properly. And I want when right click checked.

Please check my code:

<?php if (isset($sub_folders)) { ?>
<?php foreach ($sub_folders as $sub_folder) { ?>
<tr id="<?php echo $sub_folder['id']; ?>" class="check <?php if (($sub_folder['is_file']) == 0) { ?>folder-row<?php } ?> td_bgcolor" >
<td><input type="checkbox" id="<?php echo $sub_folder['id']; ?>" class="check_row selected_check"></td>
<td style="color: #c9302c"><b class="<?php if (($sub_folder['is_file']) == 0) { ?> glyphicon <?php if (($sub_folder['is_favorite']) == 1) { ?><?php echo 'glyphicon-star' ?><?php } else { ?><?php echo 'glyphicon-star-empty' ?><?php } ?><?php } ?> add-to-fav" title="Add To Favorite" id="<?php echo $sub_folder['id']; ?>"></b></td>
<td class="<?php if (($sub_folder['is_file']) == 0) { ?> vf-icon-folder <?php } else { ?> glyphicon glyphicon-file <?php } ?>"></td>
<td><?php if (($sub_folder['is_file']) == 0) { ?><a class="folder_click" id="<?php echo $sub_folder['id']; ?>"><?php } ?><?php echo $sub_folder['client_name']; ?></a></td>
<td class="text-center"><?php
if (isset($sub_folder['file_size'])) {
echo $sub_folder['file_size'];
}
?></td>
<td class="text-center"><?php if ($sub_folder['updated_date'] != '0000-00-00 00:00:00') { ?><?php echo date('d-m-Y H:m:s', strtotime($sub_folder['updated_date'])); ?><?php } else { ?><?php echo '-'; ?><?php } ?></td>
<td class="text-center"><?php echo $sub_folder['created_user']->first_name; ?> <?php echo $sub_folder['created_user']->last_name; ?></td>
<td></td>
</tr>
<?php } ?>
<?php } ?>


This is my script

$('.check').bind("contextmenu", function (event) {
event.preventDefault();

$(".custom-menu").finish().toggle(100).
css({
top: event.pageY + "px",
left: event.pageX + "px"
});
});

$(document).bind("mousedown", function (e) {

if (!$(e.target).parents(".custom-menu").length > 0) {

$(".custom-menu").hide(100);
}
});

$(".custom-menu li").click(function () {

switch ($(this).attr("data-action")) {
case "copy_move":
alert("copy_move");
break;

case "view_details":
alert("view_details");
break;

case "rename":
alert("rename");
break;

case "delete":
alert("delete");
break;
}
$(".custom-menu").hide(100);
});

Answer

you have to use contextmenu() method for right click on tr.

$('tr.check').contextmenu(function(e){
   $(this).find('input[type="checkbox"].check_row').prop('checked',true);
   return false; // not display default menu.
});