Switching a button's appearance by clicking on a different row

This form contains a toggle edit feature.

<% while(resultset.next()){ %>
<form method='POST' class="formfield" action='EditCompany'>
<tbody>
<tr align='center' class='form'>
<td><%= no %><input type='hidden' class='form_id_data' name='form_id_data' value='<%= resultset.getString(1)%>'></td>
<td><input class="form-control aaa" type="text" disabled="disabled" value='<%= resultset.getString(2)%>' name='company_name'></td>
<td><input class="form-control aaa" type="text" disabled="disabled" value='<%= resultset.getString(3)%>' name='city'></td>
<td><input class="form-control aaa" type="text" disabled="disabled" value='<%= resultset.getString(4)%>' name='state'></td>
<td><input class="form-control aaa" type="text" disabled="disabled" value='<%= resultset.getString(5)%>' name='zipcode'></td>
<td><input class="form-control aaa" type="text" disabled="disabled" value='<%= resultset.getString(6)%>' name='branch'></td>
<td><input class="form-control aaa" type="text" disabled="disabled" value='<%= resultset.getString(7)%>' name='address'></td>
<td>
<a class="edit"><span class='glyphicon glyphicon-pencil'></span></a>
   <input type='button' class='save' data-toggle="modal" data-target="#confirm-edit" value='Save'> 
    <a class="cancel"><span class='glyphicon glyphicon-remove'></span></a>
<td><a href="#" data-href="DeleteCompany?id=<%= resultset.getString(1)%>" data-toggle="modal" data-target="#confirm-delete"><span class="glyphicon glyphicon-trash"></span></a></td>
</tr>
</tbody>
</form>
<% no++; } %>

This section contains the JavaScript toggle feature for the Edit, Save, and Cancel buttons.

<!-- Toggle Edit -->
<script>
$('.edit').click(function() {
    $(this).hide();
    $('.form').find('.aaa').attr('disabled',true);
$(this).closest('tr').find('.aaa').attr('disabled',false);
    $(this).siblings('.save, .cancel').show();
});
</script>
<script>
$('.cancel').click(function() {
$('.form').find('.aaa').attr('disabled',true);
    $(this).siblings('.edit').show();
    $(this).siblings('.save').hide();
    $(this).hide();
});
</script>

    <!-- Modal Edit Alert Script class Save-->
    <script type="text/javascript">
    $('#submit').click(function(){
        $('.formfield').submit();
});
    </script>

There is an issue with the toggle edit. When I click edit on the first row, the 'Save' button remains visible even when editing the second row. Here is an example:

https://i.sstatic.net/uPpP0.png

How can I get the 'Save' button in the first row to revert back to the edit button?

Answer №1

This solution might be useful for you.

Highlighted Code:

$('.edit').click(function () {
      $(this).hide(); 
      $(this).siblings('.edit').show();
      var trs = $(this).closest('tr').siblings();          
      $(trs).each(function () {
          //Check for the save and cancel button and hide them.
          var saveCancel = $(this).children().eq(7).find('.save, .cancel');
          if(saveCancel.length)
              saveCancel.hide();
      });                             
      $('.form').find('.aaa').attr('disabled', true);
      $(this).closest('tr').find('.aaa').attr('disabled', false);
      $(this).siblings('.save, .cancel').show();
  });

REVISION:

VIEW UPDATED SAMPLE

Modified Code Snippets:

$('.edit').click(function () {
    $(this).hide();//hide the edit button
    var trs = $(this).closest('tr').siblings(); //get the sibling TR elements of clicked row.
    //loop every TR elements
    $(trs).each(function () {
        var saveCancel = $(this).children().eq(7).find('.save, .cancel');//get the children of TR element that is TD. In which go to the 7th TD. Because that is the TD which holds edit, save and cancel buttons. From that TD find for save and cancel buttons.
        //Then check for the presence of save and cancel buttons.
        if (saveCancel.length && saveCancel.is(':visible')) {
            saveCancel.hide();//And then hide the both save and cancel buttons.
            $(saveCancel).siblings('.edit').show();//After hiding the save & cancel buttons, display their's edit button.
        }
    });        
    $('.form').find('.aaa').attr('disabled', true);
    $(this).closest('tr').find('.aaa').attr('disabled', false);
    $(this).siblings('.save, .cancel').show();
});

Feel free to reach out if you need further assistance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a minigame using JQuery (Javascript)

I am currently developing a collection of mini-games for my big project. The approach I've taken in creating this mini-game is similar to one I used previously. However, unlike my previous version which only had one PuzzleContainer, this new iteration ...

"Optimizing reload time for DIV content with jQuery's .load function is proving to be

I am currently facing an issue with a div that displays data from a database and a form that updates item quantities. After submitting the form, the div refreshes while a modal with a bootstrap spinner pops up to indicate it is loading. The problem arises ...

What is the best way to assign multiple event handlers to Solid.js components within a single HTML element?

Introduction I am currently exploring Solid.js and facing a challenge in adding multiple event handlers to the same HTML element from different components using JSX. While it's straightforward in Vue, I have noticed that Solid.js overrides events bas ...

What is the most efficient way to cycle through HTML image elements and display a unique random image for each one?

I'm currently working on coding a simple game that involves guessing the Hearthstone card based on its flavor text. I plan to add a button later to progress in the game, but I haven't implemented that yet. To start, I created 4 image elements an ...

What is the reason behind only the initial click boosting the vote count while subsequent clicks do not have the same

In this snippet of code: //JS part echo "<script> function increasevotes(e,location,user,date,vote) { e.preventDefault(); var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState ...

Exploring intricate designs using HTML and Javascript

After extensive experience with both WPF and HTML5 JavaScript, one thing that stands out to me is the clear organization provided by XAML's defined panels. Grid, StackPanel, DockPanel, WrapPanel, and others offer a straightforward way to achieve consi ...

Choosing an option beforehand using angular-ui-select2 version 0.0.5

I'm struggling with setting a default option in a select2 dropdown using Angular and an ng-model. Here's my implementation: Angular controller code snippet $scope.filter = { searchValue: '', departmentId: 'Department2' } ...

Streaming audio from a microphone to speakers on Ubuntu using HTML5

Currently diving into html5 and eager to experiment with playing back what I say on a microphone through speakers. Here is the JavaScript code I've put together: navigator.getUserMedia = navigator.getUserMedia ||navigator.webkitGetUserMedia || naviga ...

Discovering the method to extract a specific section from a lengthy string

Looking to extract phone numbers from an HTML source code using PHP? Each phone number in the code starts with 'phone=' and ends with %. For example, consider the following sample HTML code: b2e1d163b0b4dc6ebfa5&amp;t=s&amp;phone=9535503 ...

Ways to enlarge image size without compromising the image resolution?

My image is in PNG format or as a blob. It has dimensions of 800px by 600px. However, when I try to resize it using various canvas methods like the one mentioned in this Stack Overflow thread: Resize image, need a good library, it loses quality. I wou ...

Table header containing the weekdays for a jQuery calendar display

I need some assistance in creating a basic monthly calendar using a table. Check out this demo: www.jsfiddle.net/pzdw0s2n/1/ ...

The ASP.NET Core MVC controller encounters a null input parameter when called by an ajax request

Here is the scenario involving an ajax call: ... $("#testBtn").click(function (e) { e.preventDefault(); $.ajax({ url: "/profile/GuestList", data: {"keytype": "1"}, method: "POST&q ...

Utilizing flot.js to showcase a funnel chart with an external JSON file as the data source

Trying to create a funnel chart using the Flot.js library with an external JSON file as input. The JSON file is being fetched successfully, but the chart is not being plotted. [ { "data": 10, "label": "a" }, { "data": 81, "label": "b ...

Input specific ng-if conditions

I'm a bit confused about the conditions for my ng-if and could use some assistance. I have a form on my page that is rendered using ng-repeat and a couple of custom filters. You can check out this plunker. The issue I'm facing is that I need to p ...

Troubleshooting await and async functions in Express.js applications

How can I create an array like the one shown below? [{ "item":"item1", "subitem":[{"subitem1","subitem2"}] },{ "item":"item2", "subitem":[{"subitem3","subitem4&q ...

Discover a scenario in which an identification number is contained within an array

For my Node.js + MongoDB project, I am utilizing sails.js and am currently faced with the challenge of loading all the groups associated with a particular user. Each Group object contains an array of user IDs as follows: Group { users: array } I' ...

How to retrieve a JSON item without knowing the name of the parent key

When requesting JSON from Wikipedia's API, the URL is: http://en.wikipedia.org/w/api.php?action=query&prop=description&titles=WTO&prop=extracts&exsentences&explaintext&format=json This is how the response is structured: { ...

What is the process for including headers while establishing a connection to a websocket?

After configuring a websocket topic using Spring websocket on the server side, we implemented the client side with Stomp.js to subscribe to it. Everything was functioning correctly when connecting directly to the websocket service. However, we decided to i ...

Updating button appearance when clicked using Vue.js and Tailwind CSS

As part of my learning journey, I have expanded this code to enhance my frontend skills. While I am aware that the code can be significantly shortened, I am focused on learning and broadening my experience in frontend development. The code below functions ...

Deactivate toggle effect by clicking on a different link

My existing JavaScript code changes the background color of a link when it is clicked: $(".button").click(function(){ $(this).css('background-color', '#555'); }); While this functionality works, I am looking to have the color tog ...