Editable popup textarea for table td

Viewed 189

I have a textarea in the td cell of each row in column 3, to hold description specific to each row. When the user clicks on the td, the current description in the textarea inside the td should be copied over to the textarea inside #div_toggle

Here is what I am trying to accomplish.

The user would make changes to the description in #div_toggle, and when done, will click 'X' to close the div. This should cause the contents to be transferred from the textarea in #div_toggle to the td cell textarea.

Would you be able to help me achieve this goal? Or am I complicating this? Is there a better approach? Below is the code I have thus far, but it does not work as desired or described above. Please help. Best regards.

<!DOCTYPE html>
<html>
<head>

    <style>
        th,
        td {
            border: solid 1px lightgrey;
        }

        #div_toggle {
            display: none;
            border: 1px solid black;
            margin: 10px;
        }

        #div_toggle textarea {
            width: 200px;
            height: 150px;
            border: 3px solid #cccccc;
            padding: 5px;
            font-family: Tahoma, sans-serif;
        }

        #close_text {
            position: absolute;
            right: 27px;
            cursor: pointer;
            padding: 5px;
            background: #cfd0d1;
            border-radius: 4px;
        }

    </style>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">

    <script>

        $(document).ready(function() {

            //Show textarea
            $('.cell_id').click(function() {
                $('#div_toggle').slideToggle('slow');
            });

            //Close textarea
            $('#close_text').click(function() {
                var tbl = $('#itemtable');
                var rows = $('tr', tbl);
                //get toggle div text area contents into td cell textarea
                rows.eq(clickedrowindex).find('td:nth-child(3)').text() = $('#div_toggle textarea#notescopy').val();                
                $('#div_toggle').slideToggle('slow');
            });

            var clickedrowindex;
            $('#itemtable').find('tr').click( function(){
                clickedrowindex = $(this).closest('tr').index();
                //get td cell textarea contents into the toggle div text area
                var notestext = $(this).find('td:nth-child(3)').text();
                $('#div_toggle textarea#notescopy').val(notestext);
            });

        });

    </script>
</head>

    <body>
        <div class="left" style="margin-top:5px; border: solid #666 1px;">
            <table id="itemtable" class="" style="width: 300px; margin: 10px;">
                <thead style="color:black;">
                <tr>
                    <th>Year</th>
                    <th>Model</th>
                    <th>Description</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td> 2013</td>
                    <td> Toyota</td>
                    <td class='cell_id'><textarea name='reqnotes'></textarea></td>
                </tr>
                <tr>   
                    <td> 2001</td>
                    <td> Honda</td>
                    <td class='cell_id'><textarea name='reqnotes'></textarea></td>                
                </tr>
                </tbody>
            </table>
    
            <div id="div_toggle"><textarea id='notescopy'></textarea>
                <span id="close_text" title="Click to close">X</span>
            </div>
        </div>
    </body>
  
</html>
2 Answers

You do not need that much of code and its can simplified to just two functions to achieve your desired results.

Firstly, we just need to make sure that we save the our current target (td > textarea) in variable and use that variable to assign val to the textarea accordingly.

Also, we need to use a class .div_toggle selector not an id #div_toggle - Since id will only pick the element which is found firstly but in our case we need to change value dynamically on each slideDown and SlideUp event.

Lastly, for this you need to use slideDown and slideUp on X button click. Its work the same way as slideToggle. Using slideToggle will create a weird behaviour.

When you click the X the content you typed in the toggle div textarea will be transfered to the td you clicked on as your target

Live Working Demo:

$(document).ready(function() {

  let currentTar; //save current target
  let divToggle = $('.div_toggle') //get element

  //Show textarea
  $('.cell_id').click(function(event) {
    currentTar = event.currentTarget
    divToggle.slideDown('slow');
    let getText = $(this).find('textarea').val()
    divToggle.find('textarea').val(getText)

  });

  //Close textarea
  $('#close_text').click(function() {
    divToggle.slideUp('slow');
    let assignVal = divToggle.find('textarea').val();
    $(currentTar).find('textarea').val(assignVal)
  });
});
th,
td {
  border: solid 1px lightgrey;
}

.div_toggle {
  display: none;
  border: 1px solid black;
  margin: 10px;
}

.div_toggle textarea {
  width: 200px;
  height: 150px;
  border: 3px solid #cccccc;
  padding: 5px;
  font-family: Tahoma, sans-serif;
}

#close_text {
  position: absolute;
  right: 27px;
  cursor: pointer;
  padding: 5px;
  background: #cfd0d1;
  border-radius: 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <div class="left" style="margin-top:5px; border: solid #666 1px;">
    <table id="itemtable" class="" style="width: 300px; margin: 10px;">
      <thead style="color:black;">
        <tr>
          <th>Year</th>
          <th>Model</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td> 2013</td>
          <td> Toyota</td>
          <td class='cell_id'><textarea name='reqnotes'>Test</textarea></td>
        </tr>
        <tr>
          <td> 2001</td>
          <td> Honda</td>
          <td class='cell_id'><textarea name='reqnotes'>Foo</textarea></td>
        </tr>
     <tr>
      <td> 2040</td>
      <td> Elon Musk</td>
      <td class='cell_id'><textarea name='reqnotes'>Tesla</textarea> 
 </td>
    </tr>
      </tbody>
    </table>

    <div class="div_toggle"><textarea id='notescopy'></textarea>
      <span id="close_text" title="Click to close">X</span>
    </div>
  </div>
</body>

A few minor changes. Utilize $(this).val() instead of doing a find for the closest :)

You'll notice the code is much cleaner as well.

$(document).ready(function() {

  $("textarea").click(function(){
    var contents = $(this).val();
    $('#notescopy').val(contents);
  });

  //Show textarea
  $('.cell_id').click(function() {
    $('#div_toggle').slideToggle('slow');
  });

  //Close textarea
  $('#close_text').click(function() {
    $('#div_toggle').slideToggle('slow');
  });
});
th,
td {
  border: solid 1px lightgrey;
}

#div_toggle {
  display: none;
  border: 1px solid black;
  margin: 10px;
}

#div_toggle textarea {
  width: 200px;
  height: 150px;
  border: 3px solid #cccccc;
  padding: 5px;
  font-family: Tahoma, sans-serif;
}

#close_text {
  position: absolute;
  right: 27px;
  cursor: pointer;
  padding: 5px;
  background: #cfd0d1;
  border-radius: 4px;
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">


</head>

<body>
  <div class="left" style="margin-top:5px; border: solid #666 1px;">
    <table id="itemtable" class="" style="width: 300px; margin: 10px;">
      <thead style="color:black;">
        <tr>
          <th>Year</th>
          <th>Model</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td> 2013</td>
          <td> Toyota</td>
          <td class='cell_id'><textarea name='reqnotes'></textarea></td>
        </tr>
        <tr>
          <td> 2001</td>
          <td> Honda</td>
          <td class='cell_id'><textarea name='reqnotes'></textarea></td>
        </tr>
      </tbody>
    </table>

    <div id="div_toggle"><textarea id='notescopy'></textarea>
      <span id="close_text" title="Click to close">X</span>
    </div>
  </div>
</body>

</html>

Related