jquery - Enable Disable Controls in a table row

Jquery - Enable Disable Controls in a table row

To enable and disable controls in a table row using jQuery, you can add a class to the row to indicate its enabled or disabled state, and then use this class to selectively enable or disable controls within that row. Here's an example:

HTML:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Enable Disable Controls in a Table Row</title> <!-- Include jQuery --> <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script> <!-- Include your custom JS file --> <script src="scripts.js"></script> </head> <body> <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Actions</th> </tr> </thead> <tbody> <tr class="enabled"> <td><input type="text" value="John" class="name-input"></td> <td><input type="text" value="john@example.com" class="email-input"></td> <td> <button class="edit-btn">Edit</button> <button class="save-btn" disabled>Save</button> </td> </tr> <!-- Add more rows as needed --> </tbody> </table> <!-- Include your custom JS file --> <script src="scripts.js"></script> </body> </html> 

JavaScript (scripts.js):

// scripts.js $(document).ready(function() { // Enable or disable controls in a table row based on its state function toggleControls(row, isEnabled) { // Find input elements within the row var inputs = row.find('input'); // Find buttons within the row var buttons = row.find('button'); // Enable or disable inputs based on the isEnabled parameter inputs.prop('disabled', !isEnabled); // Enable or disable buttons based on the isEnabled parameter buttons.prop('disabled', !isEnabled); } // Example: Disable controls in the first row initially toggleControls($('tr.enabled'), false); // Example: Enable controls when clicking the "Edit" button $('.edit-btn').on('click', function() { var row = $(this).closest('tr'); toggleControls(row, true); }); // Example: Disable controls when clicking the "Save" button $('.save-btn').on('click', function() { var row = $(this).closest('tr'); toggleControls(row, false); }); }); 

In this example:

  • The toggleControls function takes a table row and a boolean parameter (isEnabled) to enable or disable controls within that row.
  • Initially, the controls in the first row are disabled using toggleControls($('tr.enabled'), false);.
  • When the "Edit" button is clicked, the controls in the corresponding row are enabled using toggleControls(row, true);.
  • When the "Save" button is clicked, the controls in the corresponding row are disabled using toggleControls(row, false);.

Adjust the code based on the structure and requirements of your specific table and controls.

Examples

  1. "Enable/Disable All Controls in a Table Row"

    • Description: Toggle the ability to interact with all controls (inputs, buttons, etc.) in a table row.
    // Assuming 'tableRow' is the table row you want to enable/disable controls in function enableDisableControls(enable) { tableRow.find(':input').prop('disabled', !enable); } 
  2. "Enable/Disable Specific Input in a Table Row"

    • Description: Toggle the ability to interact with a specific input element in a table row.
    // Assuming 'inputElement' is the specific input you want to enable/disable function enableDisableInput(enable) { inputElement.prop('disabled', !enable); } 
  3. "Enable/Disable Button Click in a Table Row"

    • Description: Toggle the ability to click a button in a table row.
    // Assuming 'buttonElement' is the button you want to enable/disable function enableDisableButtonClick(enable) { buttonElement.prop('disabled', !enable); } 
  4. "Toggle Enable/Disable Based on Checkbox State"

    • Description: Enable or disable controls in a table row based on the state of a checkbox.
    // Assuming 'checkboxElement' is the checkbox triggering the enable/disable action function toggleEnableDisableOnCheckbox() { var enable = checkboxElement.prop('checked'); tableRow.find(':input').prop('disabled', !enable); } 
  5. "Enable/Disable Controls Conditionally"

    • Description: Enable or disable controls based on a specific condition.
    // Assuming 'condition' is the condition for enabling/disabling controls function enableDisableControlsConditionally() { var enable = condition; tableRow.find(':input').prop('disabled', !enable); } 
  6. "Enable/Disable Controls on Row Click"

    • Description: Toggle the ability to interact with controls in a table row when the row is clicked.
    // Assuming 'tableRow' is the table row you want to enable/disable controls in tableRow.on('click', function () { var enable = !tableRow.hasClass('disabled'); tableRow.find(':input').prop('disabled', !enable); }); 
  7. "Enable/Disable Controls on External Button Click"

    • Description: Enable or disable controls in a table row when an external button is clicked.
    // Assuming 'tableRow' is the table row you want to enable/disable controls in $('#externalButton').on('click', function () { var enable = !tableRow.hasClass('disabled'); tableRow.find(':input').prop('disabled', !enable); }); 
  8. "Enable/Disable Controls After AJAX Request"

    • Description: Enable or disable controls in a table row after an AJAX request.
    // Assuming 'tableRow' is the table row you want to enable/disable controls in $.ajax({ // Your AJAX configuration here success: function () { var enable = true; // Set based on your condition tableRow.find(':input').prop('disabled', !enable); } }); 
  9. "Enable/Disable Controls on Input Change"

    • Description: Toggle the ability to interact with controls in a table row based on the change of an input.
    // Assuming 'inputElement' is the input triggering the enable/disable action inputElement.on('change', function () { var enable = /* your condition based on input change */; tableRow.find(':input').prop('disabled', !enable); }); 
  10. "Enable/Disable Controls with Animation"

    • Description: Add animation effects when enabling or disabling controls in a table row.
    // Assuming 'tableRow' is the table row you want to enable/disable controls in function enableDisableControlsWithAnimation(enable) { var duration = 500; // Set your desired animation duration tableRow.find(':input').prop('disabled', !enable).fadeTo(duration, enable ? 1 : 0.5); } 

More Tags

animated-gif cursor-position uisearchcontroller quartz.net fragmentpageradapter levenshtein-distance disable lightbox system-verilog ssim

More Programming Questions

More Mortgage and Real Estate Calculators

More Other animals Calculators

More Chemistry Calculators

More Dog Calculators