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

Is it possible to display an element within a designated div using jQuery?

I am working with an element that I want to display in the middle of the page using a for cycle so it can be populated with the correct information. This particular element is hidden but should appear on hover. However, it doesn't look very aesthetica ...

Different ways to analyze elements from 2 arrays to hone in on specific values

I'm really struggling to solve this issue. I have two arrays - one for cars and the other for active filters. The cars array consists of HTML li tags with attributes like car type, number of seats, price, etc. On the other hand, the active_filters arr ...

Load data asynchronously using a promise in select2

Is there a way to load options for select2 asynchronously without using ajax requests? I want to retrieve values from a promise object instead. Currently, my code loads data in the query function, but this means that it queries the data with every keystrok ...

What could be causing the issue with clicking on children of jstree not working?

I am encountering an issue with jstree. Once the data is changed, the click event does not work for every subsequent click. I find that I need to refresh the page in order to make it work. Here is a link to the jsfiddle: http://jsfiddle.net/2Jg3B/2121/ I ...

disabling responsiveness in Bootstrap 2

I am facing an issue with Bootstrap version 2 where I am unable to fix the grid layout. Even after wrapping all my HTML elements in a container, the layout still behaves unpredictably on different screen sizes. Here is a snippet of my code: <header> ...

Insert a fresh item into the existing unordered list

Every time I enter something in the prompt, it shows up as "undefined". What I actually want is for whatever I type into the prompt to be added as a new list item. For instance, if I type "Food" in the prompt, I expect to see "Food" appear on the second li ...

Div failing to expand to fit its contents

I'm having trouble getting my info-container div to adjust its height based on its contents. I suspect that the floating div within it might be causing the issue, but I'm not certain. I've included a link to jsfiddle where you can view the C ...

Creating a process to produce a random number and send it directly to an automated email

I'm currently utilizing a form builder from jqueryform.com to construct a registration form. My goal is to have each registered user assigned with a unique account number, which will be a randomly generated 6-digit number. Additionally, I want the pro ...

Implementing mixin functions in vue.router routes with Vue.js

Is there a way to dynamically change the title of the window based on each route? I have included a meta: { title: ... } object in each child object within the routes: []. Here is an example: routes: [ { path: 'profile/:id', name: 'Prof ...

The functionality of IE's .attr disabled feature appears to be ineffective

My function to check if a product is available overnight is not functioning properly in Internet Explorer, although it works fine in other browsers. function checkOvernight() { id = $_2("[name='product']").val(); if(id.length > 0) { ...

Is it possible to process HTML and JavaScript as a request in JMeter?

After receiving a response, I noticed that the HTML body contains a hidden form along with an internal JavaScript function to submit the form (view snapshot here). Is there a method in JMeter to execute JavaScript code that directly submits a form? I am ...

Refreshing a Vue JS component

As a beginner in VueJs, I am facing an issue with reloading a component and table when a refresh button is clicked. I have tried using the forceUpdate method and changing keys, but it has not been successful so far. If anyone has any suggestions on how to ...

Perform a Selenium action to click on each individual HTML 'a' tag on

I am attempting to automate clicking on all the "a" tags within a website using Selenium in Python. However, I found that all the "a" tags I want to click have the same structure as shown in the code snippet below. I tried clicking them by their class si ...

Maintain the previous droppable positioning after refreshing the page

I am encountering an issue with the .droppable event. I have set up two sections where I can move elements, but every time the page is refreshed, the positioning of the elements reverts to the initial position. How can I maintain the last positioning of th ...

When first accessing the page, the map may not load properly. However, a simple refresh of the page in VueJS should resolve this issue and allow the

After initially loading the page, the map may not work properly in Vue.js. However, refreshing the page fixes this issue. Can anyone provide assistance with this problem? Here is the relevant code: mounted() { this.geolocate(); }, methods: { ...

An in-depth guide to effectively unit testing a Node.js Express application

Looking to kickstart unit testing in my Node Express project. What's the most straightforward and convenient approach for this? ...

Struggling to find the element using Selenium

Hey there, I'm struggling with identifying the input id button in my HTML code while using Selenium through Java. The error message says it's unable to locate the element. Can anyone provide some guidance? I've already tried using xpath and ...

What is the process for loading a font file in Vue.js and webpack?

I've done a lot of research, but I couldn't find any links that show me exactly how to add fonts in VueJS. This is the method I'm using to import the font in my LESS file: @font-face { font-family: "Questrial"; src: url("../../fonts/Que ...

Having trouble retrieving alert message after submitting form using jquery

Trying to submit a form using jQuery, but when clicking on the submit button it displays a blank page. I understand that a blank page typically means the form has been submitted, but I want to show an alert() for testing purposes instead. However, it only ...

Transform buffer information into a visual representation

How can I convert the buffer data into an image so that when I loop through the results and render it in the img src, the user will be able to see the image? I am currently using ejs for rendering. <span> <img class="user-with-avat ...