Interactive pop-up text box for data cells in a table

I have implemented a textarea in the td cell of each row within column 3 to store description specific to that row. The goal is for the current description in the td's textarea to be copied over to the textarea located inside #div_toggle when the user clicks on the td.

Here's what I aim to achieve:

The user can modify the description in #div_toggle, and upon completion, they can click 'X' to close the div. This action should transfer the revised contents from the textarea in #div_toggle back to the td cell's textarea.

Can you assist me in accomplishing this task? Am I making this too complicated, or is there a simpler approach?

Below is the code I've developed so far. Unfortunately, it does not function as intended or described above. Your guidance would be greatly appreciated.

<!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>

Answer №1

To achieve the desired results with minimal code, you can simplify it by using just two functions.

Start by saving the current target (td > textarea) in a variable and use that variable to assign the value to the textarea.

Make sure to use a class selector (.div_toggle) instead of an id selector (#div_toggle) for dynamic value changes during slideDown and slideUp events.

For functionality, utilize slideDown and slideUp on X button click instead of slideToggle to prevent any unexpected behavior.

Upon clicking X, the text entered in the toggle div textarea will be transferred to the clicked td as the target location.

See live demo below:

$(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>

Answer №2

Make a few small adjustments by using $(this).val() instead of searching for the closest element with the find method :)

You'll see that the code looks much neater this way.

$(document).ready(function() {

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

  //Display 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>

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

I am unable to view the input appearing inside the input box on my React/HTML application

I have been developing a React app for building portfolios, but I am facing an issue where I cannot see any text that I type into my input boxes. Can someone please help me troubleshoot this issue? Any assistance would be greatly appreciated. I have made ...

My HTML loop is functioning properly with a variable, however, it is not working as expected

Hi there! Here is a piece of code that includes a loop with a timer. I am trying to change a variable using a button, however, it's not working as expected. <!doctype html> <html> <body> <script> var timer = 1000; var counter ...

What is the process for aligning Item1 to the left and Item2 & Item3 to the right using MUI React?

I'm working on a component that contains three different Typography components. I need to align "summary" to the left, while 'miles' and 'duration' should be aligned to the right. https://i.stack.imgur.com/7e9sY.png Here's t ...

Retrieving dates from a database and populating them into a jQuery UI Picker using PHP

I need to retrieve dates from the database using PHP and highlight them in a datepicker. Here is how I am attempting to accomplish this: HTML Date: <input type="text" id="datepicker"> // Static dates // An array of dates var eve ...

WordPress: Issue with Dropdown onchange Event Not Triggering

I'm having trouble getting an event to fire when the dropdown value changes. Can anyone help me figure out where I might be going wrong? JavaScript code jQuery(document).ready(function($){ jQuery('#_ccounts select').on('change&apo ...

Troubleshooting issue with Django development server causing HTML5 video element to become non-seekable

My Django app is currently serving a webpage with an HTML5 video element, but I've encountered a strange issue. The video.seekable property is returning a timeRanges object with a length=0, when it should actually be length=1. Unfortunately, this mea ...

Arranging elements in a manner reminiscent of the tiles in Windows 8

Click here to view an example. I'm attempting to create a layout similar to Windows 8 tiles grid, where there are three types of tile sizes available: Normal = 1x1; Wide = 2x1; and Big = 2x2. My goal is to have these tiles displayed without any em ...

Having trouble setting the CSS width to 100%

Is there a way to assert the CSS width of an element in NightwatchJS to be 100% without hard-coding the value? Currently, when I attempt to do so, it fails and reports that the width is 196px. The specific error message is as follows: Testing if element ...

AngularJS: resolving route dependencies

I have a variable $scope.question that contains all the questions for the page. My goal is to loop through the questions page by page. To achieve this, I created a function called questionsCtrl and I am calling this function in the config while setting up ...

The input field within the mat-form has been styled to match the background of the page using CSS

I am currently facing an issue with the code below: Html: <form fxLayout="column" fxLayoutAlign="center center" [formGroup]="deleteForm" (ngSubmit)="onSubmitForm()"> <mat-form-field color="accent" appearance="outline"> <input ...

jQuery eliminates initial div just a single time

Here is a function I have created: function removeDiv() { var topmost = jQuery('.xx'); var totContent = topmost.find('.zz').length; var $target = jQuery('.xx').find('.zz').eq(0); if(totCont ...

Convert string query into an array in PHP containing keys and values

When passing the given query string into a PHP method searchFilterKeyword=ff&searchFilterDateEntered=07%2F29%2F2019&searchFilterStatus=1&searchFilterQuarantineStatus=&searchFilterSize=&searchFilterKennel=&searchFilterType= How can ...

Can you explain the significance behind this unorthodox CSS code?

Assisting a customer in updating their Shopify theme. The previous designer used an unconventional method to establish the grid system. I require assistance in decoding the code. I came across an old article on this topic but still couldn't grasp it. ...

Exploring Bootstrap's Product sample and attempting to understand the process of incorporating images

Currently, I am utilizing Bootstrap 4 and taking advantage of their Product example as a foundation: https://getbootstrap.com/docs/4.4/examples/product/ My goal is to transform the white and gray background sections into actual images of the product. The ...

Tips for utilizing javascript document.createElement, document.body, and $(document) in an HTML web resource for Microsoft CRM code reviews

Let me start off by saying that I am not a regular blogger and I am feeling quite confused. If my question is unclear, please provide guidance for improvement. I recently submitted a Microsoft CRM PlugIn to the Microsoft Code Review. I am new to Javascrip ...

Unable to access URL through AppGyver's rootview

As per the information provided in their guide: All location attributes can be either Supersonic routes or URLs. After adding the code to my structure.coffee file, only the initial splash screen appears and nothing else loads: rootView: location ...

Once the record has been successfully inserted on the index.aspx page, the function will redirect to the Ajax success method in Asp

When I insert a record on the index.aspx page and redirect to the ajax success function, the message should display alert("success"). I created the function addProject() to handle this task. Surprisingly, there were no errors during the process of adding t ...

The embedded Google iframe is causing unnecessary padding at the bottom of the page

Below is a snippet of HTML code: <div class="google-maps"> <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d109782.8671228349!2d76.62734023564995!3d30.69830520749905!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x390fee90 ...

Encode JavaScript field without affecting the appearance

I need to encode a specific search field before submitting it as a $_GET request. To do this, I am using the encodeURIComponent() function on that field before the form is submitted. $('#frmMainSearch').submit(function() { var field = $(this ...

How come my footer is appearing at the top of my webpage?

I have encountered a problem where the footer is displaying on top of my page, despite not using any floats, wrappers, or grids in my code. The code snippet can be found below this text. Could someone assist me in identifying why this issue is occurring ...