Tips for positioning text input fields so that they are neatly aligned within the borders of a table

Currently, I am working on implementing Jinja code in my Flask application. I have a

<table class="table table-hover">
  <thead>
    <th scope="col" class="text-primary">Name</th>
    <th scope="col" class="text-primary">Description</th>
  </thead>
  <tbody class="text-secondary">
    {% for item in char.inventory %}
    <tr id="{{item.itemid}}_row">
      <div>
        <td id="{{item.itemid}}_name">{{item.name}}</td>
      </div>
      <div>
        <td id="{{item.itemid}}_description">{{item.description}}</td>
      </div>
      <td class="text-right"><button id="{{item.itemid}}" class="btn btn-outline-info" onclick="itemeditor(this)">edit</button>
      </td>
    </tr>
    {% endfor %}
  </tbody>
</table>

This loop generates a table that is styled with Bootstrap framework. By clicking the button, JQuery code is triggered to change table entries into text inputs with corresponding values.

function itemeditor(elem) {
  self = $(elem);
  itemid = self.attr("id");
  itemnamenode = $("#" + itemid + "_name")
  itemdescriptionnode = $("#" + itemid + "_description")

  itemnamefield = $('<input type="text" style="display : inline;" size="50" class="form-control" />')
  itemnamefield.val(itemnamenode.text())

  itemdescfield = $(('<input type="text" style="display : inline;" size="50" class="form-control" />'))
  itemdescfield.val(itemdescriptionnode.text())


  itemnamenode.replaceWith(itemnamefield)
  itemdescriptionnode.replaceWith(itemdescfield)
}

The issue arises when the text inputs created by JQuery expand to full width and stack vertically instead of aligning horizontally like the original table entries.

Table state before button click

Table state after button click

Is there a way to adjust the Bootstrap/CSS settings so that the text inputs stay aligned within the table structure? Thank you for any assistance provided.

Answer №1

Your current table setup is incorrect. It's not valid to have a div as a direct child of a tr.

Consider restructuring it like this:

<tbody class="text-secondary">
    {% for item in char.inventory %}
    <tr id="{{item.itemid}}_row">
        <td>
            <div id="{{item.itemid}}_name">{{item.name}}</div>
        </td>
        <td>
            <div id="{{item.itemid}}_description">{{item.description}}</div>
        </td>
        <td class="text-right"><button id="{{item.itemid}}" class="btn btn-outline-info"
                onclick="itemeditor(this)">edit</button>
        </td>
    </tr>
    {% endfor %}
</tbody>

Another approach could be simplifying the structure by using only input elements, initially disabled and activated only during editing mode. Remember to style disabled inputs to appear as regular text.

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

Troubleshooting IE7's overflow problem

In my website, I have a ul element with overflow-y set to auto for displaying a nice scroll bar with all the li elements. However, in IE7, this setting is ignored and the list overflows outside the container. To see the issue directly, you can visit http: ...

CSS: Positioning footer at the bottom of ASP webpages (not fixed to the bottom of the screen)

After researching various methods for positioning footers in webpages using CSS, I have come across several solutions, including some on Stack Overflow. However, it seems that most of these approaches do not work with ASP pages. Therefore, my question is: ...

step by step guide on swapping a portion of a JSON string

I need to eliminate the character "C" from keys that start with C_ in the following JSON string. Here is the JavaScript object I have: var jsonData= { key1:val1, key2:val2, C_100:1, C_101:2, C_102:3, } The desired output should look like this: v ...

The Bootstrap 4 navbar-toggler-icon is missing from the screen

Check out this code snippet: https://jsfiddle.net/8tpm4z00/ <div class="container"> <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#myNavigation" aria-controls="myNavigation" aria-expa ...

Simple HTML and CSS exercise for beginners to grasp the concept

I'm looking to modify the layout of a specific webpage at based on the following instructions: First, insert this link within the <head> <link rel="stylesheet" href="https://trafficbalance.io/static/css/sdk/sdk.css"> -next, add th ...

How can I resolve the issue of React not identifying the prop on a DOM element?

Currently, I am learning React and facing an issue with a warning message: "react-dom.development.js:86 Warning: React does not recognize the isSelected prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell i ...

Is there an alternative to using CSS units of measurement when using the .css() method?

Is there a way to use .scrollTop() and .css() to adjust the margins of an element so that it floats up only when the page is scrolled, but stops at a certain margin value? I want the element to float between specific values while scrolling. The issue I am ...

Tips for retaining a variable in an ajax callback?

The implementation involves a JavaScript code that iterates through an array of "modules". It performs an ajax call for each module, retrieves the HTML content for the module, and places the module in either of the two columns within the page layout. var ...

The HTML document is having trouble establishing a connection with socketio

I currently hold an HTML file within my local file system, presented as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example of a Minimal Working File</title> ...

Ensuring the HTML remains at its original scale while scaling a div using CSS

My code consists of HTML and CSS that look like this: <div class="box" style="transform: scale(2);"> <div class="text">This is my text.</div> </div> I'm trying to figure out how to prevent the text inside the box from sca ...

What causes the slight shifting of on-screen elements when the doctype is removed from an HTML document?

During the process of converting from HTML4 to HTML5, I compared my html4 and html5 pages and noticed that removing deprecated elements after the doctype declaration was causing slight movements in on-screen elements. For example, in the code snippet below ...

Which is the better approach for performance: querying on parent selectors or appending selectors to all children?

I currently have 2 mirror sections within my DOM, one for delivery and another for pickup. Both of these sections contain identical content structures. Here's an example: <div class="main-section"> <div class="description-content"> ...

What is the best way to place a list within a <div> element?

There is a <div> with an image in the background. I now want to include a list within that div, but I'm having trouble positioning it. Check out this image for reference In the image above, you can see the background and the list, but I'm ...

The presence of text is leading to CSS placement troubles

I operate a website dedicated to comic content and am looking to incorporate text (retrieved from the database field "description") beneath each comic thumbnail. Here are my two inquiries: 1) I'm facing an issue where adding text below a comic caus ...

` How can I activate a dropdown menu based on the selection of another dropdown menu in SENCHA/ext js?`

I need to make a change in my Sencha application. I want to add a second combobox that starts off disabled, which is not an issue. However, I also need the first combobox to enable the second one when certain items (not all) are selected from the first com ...

Can you explain the purpose of jQuery's event.detail property?

When handling the click event on a button, I noticed that when the button is clicked, e.detail = 0. However, if I press enter inside a textbox and somehow trigger the button click event (asp.net), e.detail = 1. I searched through the jQuery documentation, ...

Integrate a loading screen on the website

Check out the awesome animation I created! Can this be used as a preloader on my website? Should I stick to creating a simple .gif or opt for a CSS animation instead? If it’s feasible, how do I go about integrating it into my site? Most tutorials suggest ...

Using JavaScript to block form submission based on AJAX response

I am having trouble understanding why this code is not working as expected to prevent a submit. I have made sure all the HTML elements are properly linked and all alerts display correctly. It doesn't seem like I am losing scope on the event. If anyone ...

A tutorial on utilizing fopen in PHP to write text lines to a .txt file

Struggling with PHP code utilizing fopen to write text lines into a textarea and save them to a .txt file. Below is the code I've been working on, hoping to get some assistance! <?php session_start(); if (!isset($_SESSION['username&ap ...

The parent container is being pushed down due to margin-top, despite the use of overflow:hidden

After adding a margin-top to one of my nested divs, the surrounding divs are being pushed down unexpectedly. I have searched for solutions on various forums and tried suggestions such as using overflow:hidden/auto, adding a border, or applying a margin/pad ...