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

Accessing embedded component within an Angular template

I have a ng-template that I utilize to generate a modal with a form on top of one of my other components like this: <div> <h1>Main component content...</h1> <button (click)="modals.show(newthingmodal)">Create New T ...

Ways to reload a webpage from the bottom without any animation

It seems common knowledge that refreshing a webpage on mobile devices can be done by pulling down from the top. However, I am looking to shake things up and refresh the page by pulling up from the bottom instead. And I prefer no animations in the process. ...

What is the best way to use scrollIntoView() to display an additional item at the top or bottom of the visible area

When implementing scrollIntoView() with navigation buttons (up and down), I aim to display two items at a time to signal to the user that there are more items to navigate. However, the first and last items should retain their default behavior so the user u ...

Creating a table with both a fixed header and a fixed column using only CSS

I'm looking to create an HTML table with a fixed header and fixed first column, but I want to avoid using JavaScript or jQuery to set scrollTop/scrollLeft for smooth functionality on mobile browsers. I found a solution that fixes the first column he ...

How can we reduce the use of !important in SCSS when working with React and Material UI?

In my current project, I am developing a Select component in React with Material UI. The CSS styling for the component is managed through an external SCSS sheet imported into the script file. While working on restyling the component, I found it challengin ...

What is the best way to center my navigation bar without interfering with the mobile version's JavaScript functionality?

Just starting out with web development and stack overflow, so please bear with me if I struggle to explain the issue. I came across some JavaScript to make my website responsive on small screens (mobiles). However, I am having trouble centering my top nav ...

Is it possible to craft poetic lines with indents in HTML using blockquotes, pre, or dd tags?

I am struggling to format a few lines of text with indentations using HTML. Here is an example I found: HERE Below is my attempt: <p><code>"Knowing is not enough. We</code></p> <p><blockquote><code>must apply. Wi ...

The MIME type 'text/html' is incompatible with stylesheet MIME type and is not supported

I have exhausted all possible solutions for the issue, from specifying the type for <link rel="stylesheet" href="./style.css" /> to using app.use(express.static('public')), but unfortunately, none of them seem to resolve the problem. index ...

Accessing a precise div element in a webpage

Currently, I am utilizing Vue.js and Nuxt for my web development. One issue I am facing is related to anchors. Specifically, when I navigate to mysite.com/page1#section1, I want the page to scroll to section1. In my code, I have the following snippet: < ...

Tips for creating a form-flip, similar to a card-flip effect, using web technologies

Looking to create a card flip effect similar to the one shown here. Once the sign up process is finished, the card will smoothly flip over to reveal the other side with a transitioning effect. ...

Select2 Bootstrap - Preselecting Options

I am currently utilizing Bootstrap Multiselect but I am encountering some difficulties setting the default value for the multiselect as per the documentation provided. Inside a function, I have tried the following: $("#selector").multiselect("select", ["V ...

Mastering the art of combining images and text harmoniously

I am having trouble aligning my lion image and h1 tag side by side. There seems to be an issue but I can't figure out what it is. h2 { width:50%; float:right; padding:30px 0px 0px 0px; margin:0 auto; } .lion { width:10%; float: left; paddin ...

Change the image size as you scroll through the window

While my opacity style is triggered when the page is scrolled down, I am facing an issue with my transform scale not working as expected. Any suggestions on how to troubleshoot this or any missing pieces in my code? Codepen: https://codepen.io/anon/pen/wy ...

What is the best way to set a default value for a date input field?

I am in the process of developing a form that enables users to modify the information on the form, with the initial values of the form appearing as defaults. Despite setting my variable as the value, it does not display. All other default values are visib ...

Upgrade the tablesorter to Mottie's fork

Currently, I have a functional tablesorter setup using V 1.0.3. Everything is working smoothly with selected columns, a custom image displayed, and an initial sort (with CSS styling) on a single column. The only issue I'm facing is with handling empt ...

What are the reasons behind the ineffectiveness of !important in CSS?

I am looking to increase the font-size of all text on my website to 200%, you can find some test code in this Fiddle link. If it is a PX issue, why does the code work when I add the style to "<h2>"? <div> <h2 class="test" style="font-size:300 ...

Prevent the caching of the cache manifest file in IIS to avoid issues with browser caching

Looking for some advice on adding a cache manifest to my web app. I'll be deploying the app on IIS and need help making sure "that the cache manifest file is not cacheable by HTTP semantics" (source: ) Does anybody have tips on preventing a .appcache ...

Nested Angular click events triggering within each other

In my page layout, I have set up the following configuration. https://i.stack.imgur.com/t7Mx4.png When I select the main box of a division, it becomes highlighted, and the related department and teams are updated in the tabs on the right. However, I also ...

Using jQuery, we can replace all `<span>` elements with `<img>` elements and then verify if the images have been successfully loaded

Similar Inquiry: jQuery check if image is loaded I find myself in a scenario where the following HTML structure is utilized: <div class="image"> <img class="" src="content-images/1.jpg"> <span class="slide" rel="content-images/ ...

Utilizing two distinct CSS frameworks within a single Rails application

At the moment, I have an application with its frontend built using Bootstrap 3 (leveraging the bootstrap-rails gem). Now, I am looking to incorporate the Materialize.css framework into the same app. After downloading the Materialize.css stylesheet, my conc ...