Recently, I implemented a feature in my code where clicking on an item would display a contextual menu (in the form of a div) that allows users to cut and paste list items. To control the functionality, I added a 'disabled' class to the buttons within the div that are related to the paste operation. However, I encountered an issue: even when something is selected, I couldn't remove the 'disabled' class from the buttons to enable pasting. Below is the snippet of the code:
HTML:
<div id="contextMenu" class="text-center" style="display: none; z-index: 1001; width: 242px;box-shadow:#D3D3D3 0px 0px 10px 4px; background-color: #FDFFC7; padding: 15px;">
<a class="btn btn-primary" style="margin: 3px 6px 3px 0px;" href="#" id="cut"><i class="fa fa-cut fa-lg"></i> Cut</a>
<div class="btn-group" style="margin: 3px 0px 3px 6px;">
<button type="button" class="btn btn-success disabled" id="InsertInto" title="Pastes the cut item into the selected category."><i class="fa fa-paste fa-lg"></i> Paste</button>
<button type="button" class="btn btn-success dropdown-toggle disabled" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<li><a href="#" title="Adds above the selected category" id="AddAbove"><i class="fa fa-level-up fa-lg"></i> Add Above</a></li>
<li><a href="#" title="Adds below the selected category" id="AddBelow"><i class="fa fa-level-down fa-lg"></i> Add Below</a></li>
</ul>
</div>
JS:
$(document).on('click', '.ContextMenu', function () {
event.preventDefault();
var anySelected = $('.selected').length;
if (anySelected > 0) {
$("button", "#contextManu").removeClass('disabled');
}
var widgetPosition = $('#contextMenu').parent().offset();
$('#contextMenu').css('position', 'absolute');
var currentPosition = $(this).offset();
var contextLeft = (currentPosition.left) - $('#contextMenu').width() - widgetPosition.left - 10;
var contextTop = (currentPosition.top + 80) - widgetPosition.top;
$('#contextMenu').css('left', contextLeft);
$('#contextMenu').css('top', contextTop);
$('#contextMenu').slideDown(300);
$selectedListItem = $(this).parents('div.categoryItemContainer').parent();
});
During my debugging process using Google Chrome browser, I discovered that the line $("button", "#contextManu") located two buttons within the contextMenu div but failed to make any changes to them.