A table featuring an HTML select element that allows users to choose from preset options or manually enter

My goal is to incorporate a select box where users can either choose from a set list of options or input their own custom value. The select element is nested within a table. Unfortunately, using datalist is not a viable solution for me in this case. I have adapted a method I found on HTML select form with option to enter custom value to suit my needs, and it almost works perfectly except for the fact that when 'Enter Option' (to type a custom value) is selected, the height of the cell doubles. Is there a simple CSS trick to maintain uniform table cell height? Here is the link to my code on jsFiddle. Thank you in advance for your assistance. Below is the code:


<table id="tid">
  <tbody>
    <tr>
<td>column1</td>
      <td>
        <select>
          <option class="non" value="option1">Option1</option>
          <option class="non" value="option2">Option2</option>
          <option class="non" value="option3">Option3</option>
          <option class="editable" value="">Enter Option</option>
        </select>
        <input class="editOption" style="display:none;" placeholder="Enter Value"></input>
      </td>
    </tr>
    <tr>
<td>column1</td>
      <td>
        <select>
          <option class="non" value="option1">Option1</option>
          <option class="non" value="option2">Option2</option>
          <option class="non" value="option3">Option3</option>
          <option class="editable" value="">Enter Option</option>
        </select>
        <input class="editOption" style="display:none;" placeholder="Enter Value"></input>
      </td>
    </tr>
  </tbody>
</table>

Javascript

$('#tid > tbody > tr > td:nth-child(2)').change(function() {
  var selected = $('option:selected', this).attr('class');


  if (selected == "editable") {
    $(this).find('.editOption').show();
    $(this).find('.editOption').keyup(function() {
      var editText = $(this).find('.editOption').val();
      $(this).find('.editable').val(editText);
      $(this).find('.editable').text('Enter Option');
    });

  } else {
    $(this).find('.editOption').hide();
  }
});

CSS

#tid td {
  border-collapse: collapse;
  border: 1px solid black;
  width: 80px;
  max-height: 5px;
}


.editOption {
  width: 80%;
  position: relative;
  top: -20px;
  border: 0;
  padding-left: 5px;
}

Answer №1

If you want to position the .editOption element absolutely, you can do it like this:

.editOption {
  width: 100px;
  position: absolute;
  top: 0;
  left: 0;
  border: none;
  padding-left: 10px;
}

UPDATE: For a table with multiple columns, give the td elements a non-static position so that the .editOption remains within the cell.

#myTable td {
  position: relative;
  border-collapse: collapse;
  border: 1px solid black;
  width: 100px;
  max-height: 8px;
}

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

Tips for sending an email to an address from a user input field in React.js/Express.js

I am currently developing an email application that allows users to send emails to any recipient of their choice. The challenge I'm facing is that I need a way to send emails to user-specified email addresses without requiring passwords or user.id det ...

How would you go about creating a VueJS component that displays a table with a set number of columns and automatically distributes the cells within them?

Hey there! I'm currently working with a VueJS component that looks like this: <template> <div> <table> <tbody> <tr v-for="(item, index) in items" :key="index"> <t ...

Incorporate a `fresh Audio` element into my redux repository

I'm attempting to include a new Audio element in my redux store. This is how my reducer appears: export const songsReducer = (state = {}, action) => { switch (action.type) { case "PLAY_SONG": return { ...state ...

What is the best location to create the content for a sidebar?

Currently in the process of building my new website using express. The layout consists of various "sections" such as a blog, project information, and more. I want to have a unique sidebar next to the main content for each section. For instance, in the "blo ...

exchanging a library for a different one

I'm faced with a relatively simple task here, but as I am just beginning to delve into object-oriented programming, it is proving to be quite perplexing for me. Currently, I am using the lon_lat_to_cartesian function from the following source: functi ...

Having trouble with installing node-steam on your node.js server?

I recently attempted to install node-steam and encountered a significant error. Can anyone provide guidance on how to resolve this issue? Is the mistake on my end? ... gyp ERR! configure error gyp ERR! stack Error: Can't find Python ex ...

What steps should I take to resolve this unexpected issue with svelte?

Whenever I attempt to execute the application, an error is consistently displayed to me. Here is a screenshot of the error: https://i.sstatic.net/jfo3X.png This issue always arises on the initial import type line, regardless of the content or arrangement ...

I am looking to dynamically print countries from an array in my code based on the selected option

I'm in need of some simple code assistance. I want to display a list of countries that correspond to a selected letter, but I'm struggling to do so dynamically at the moment. For example, if I select the letter A from a dropdown menu, I want the ...

A solitary block positioned at the heart of a grid - jquery mobile

Using grid in jQuery Mobile, I have designed a page and now looking to place a block exactly centralized on it. In the first grid, there are 2 blocks equally positioned. In the next grid, only one block is present and I want it to be centered. JSFiddle L ...

Is there a way to undo the CSS rotate animation that has been applied?

Struggling with reversing a CSS animation based on a class being added to a DOM node? Take a look at the code I've been using: EXAMPLE // Closed state @-moz-keyframes spin-close { 100% { -moz-transform: rotate(-0deg); } } @-webkit-keyfra ...

Hide the external div if the tab is active in Angular Bootstrap

How can I determine if a specific tab is active and then hide a div outside all tabs when it is active? Plunker: https://plnkr.co/edit/b9O9S7JxxgzhQKcKONkn?p=preview <div class="border"> Conceal me when Tab #3 is active. </div> < ...

The Ionic application encounters an issue with the $stateParams being

Can someone assist me with resolving an issue related to ionic $stateParams? Here is the state configuration: .state('tabs.categories', { url: "/categories/:parentID", views: { 'categories-tab': { templateU ...

Having trouble retrieving a dynamic name with Formcontrol error?

I keep encountering a typeError in this section of code <p *ngIf="formValue.controls['{{obj.name}}'].invalid, but when I manually enter it like this *ngIf="formValue.controls['uname'].invalid it works perfectly fine. What ...

Creating a mobile-friendly layout with 3 columns: Tips and tricks

I attempted to solve the issue independently but didn't have much success. My goal is to create a proper version for mobile users using bootstrap, however, it currently looks terrible on mobile devices. I suspect the problem lies with the div classes ...

maximum distance a button can respond to in a CSS layout

I'm trying to figure out why my CSS button has such a large clickable area. Is there a way to reduce it? When I hover over the button, the clickable area extends beyond the text, which is not ideal. Any suggestions on how to fix this without altering ...

How can I verify the presence of email and mobile numbers in my MongoDB database?

const express = require('express'); const router = express.Router(); require('../db/conn'); const User = require('../model/userSchema'); router.get('/', (req, res) => { res.send(`Hello World from the server ...

What is the best way to incorporate animation into a React state?

Looking to implement a fade-in animation for the next index but struggling with tutorials using "react transition group" that are focused on class components or redux. const AboutTestimonials = () => { const [index, setIndex] = useState<any>(0 ...

Can a substring within a string be customized by changing its color or converting it into a different HTML tag when it is defined as a string property?

Let's discuss a scenario where we have a React component that takes a string as a prop: interface MyProps { myInput: string; } export function MyComponent({ myInput }: MyProps) { ... return ( <div> {myInput} </div> ...

Troubleshooting: WordPress failing to launch Bootstrap Modal

I recently transformed my PHP website into a WordPress theme, but unfortunately, I am facing an issue with my Bootstrap modal not opening in WordPress. ***Header.php*** <a id="modal_trigger" href="#modal" class="sign-in-up"><i class="fa fa-user ...

Making a cross-browser request using JSONP, while receiving data in XML format

I am trying to extract data from a specific URL using only client-side techniques. Here is the current code I'm working with: <script type="text/javascript"> $(document).ready(function() { var data; $('#New&ap ...