A dropdown menu generated from a Google Sheet script that allows for the input of custom text alongside predefined

Currently, I am working on creating a dropdown list that not only allows me to select items but also input free text. The code below enables me to choose an item from a selected range in Google Sheets. However, I am looking for a way to incorporate the functionality to input new text as well. I have tried using Datalist without much success. Any assistance or advice on how to achieve this would be highly appreciated.

<div class="row">
        <script type="text/javascript"></script>
 
         <script>
          <?
          var sheet   = SpreadsheetApp.openById("ID").getSheetByName("Sheet");
          var lastRow = sheet.getLastRow();  
          var myRange = sheet.getRange("A2:A"+lastRow); 
          var data    = myRange.getValues();
          
          ?>
          google.script.run.withSuccessHandler(populategetEmp1).getEmp1();
         </script>  
   
    <div class="form col-md-6">
      <label for="emp1">Client</label>
        <select  id = "emp1" class="form-control" >
         <option value=""  ></option> 
         <? for (var i = 0; i < data.length; ++i) { ?>
         <option ><?!= data[i] ?></option>
         <? } ?>
        </select>
      </div>

Answer №1

It seems that your objective is to include a text input in a dropdown list.

  • You are looking to add a text input field for the dropdown list.

One suggestion could be to utilize the datalist tag as shown below:

Modified code snippet:

<script>
function test() {
  var value = document.getElementById("input").value;
  console.log(value)
}
</script>

<div class="form col-md-6">
<label for="emp1">Client</label>
<input type="text" id="input" list="emp1" placeholder="Insert text here.">
<datalist id="emp1" class="form-control" >
  <?
  var sheet   = SpreadsheetApp.openById("ID").getSheetByName("Sheet");
  var lastRow = sheet.getLastRow();
  var myRange = sheet.getRange("A2:A"+lastRow);
  var data    = myRange.getValues();
  for (var i = 0; i < data.length; ++i) { ?>
  <option value="<?!= data[i] ?>">
  <? } ?>
</datalist>
</div>
<button onclick="test()">Click</button>
  • Upon opening the HTML page, you can freely enter text into the input field.
  • By clicking the drop-down arrow on the right side of the input field, the list will appear.
  • Clicking the "Click" button will retrieve the value, which can be viewed in the console log.

Related Resource:

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

Guide to interpreting an Angular expression using traditional JavaScript conventions

I have encountered an issue with the Angular app that I am currently working on. It needs to retrieve the Google Analytics ID from Angular in order to pass it to ga('create', 'UA-XXXXX-Y') using standard JavaScript in my index.html. &l ...

Enhancing infinite scroll functionality with ajax integration

After implementing a method to enable infinite scroll on my website using the following function: window.onscroll = yHandler; function yHandler(){ var wrap = document.getElementById('wrap'); var contentHeight = wrap.offsetHeight; var yOffset = w ...

I'm encountering a Type Error message in VS Code terminal stating that converting data to string is not a function. Can someone please help me understand this type error? I am new to Node.js and

const fileSystem = require('fs'); const data = fileSystem.readFileSync('example.txt'); if (data) console.log(data.toString()); console.log('Program Ended'); This is the code I wrote: and this is the error message my terminal ...

Tips for creating clickable selections with AutoComplete JQuery in ASP.NET and C#

Currently, I am working on a project that involves implementing AutoComplete functionality in a textbox using JQuery AutoComplete. I have included the following file: jquery-ui.js The goal is to make the matched text appear in bold. For example, if I typ ...

Tips for holding off until the fetch operation is complete

Recently started working with JavaScript and facing an issue where the return part doesn't wait for my status from the fetch call. How can I delay this until the status is retrieved? Even tried adding timeouts but that didn't solve the problem. c ...

Scrolling Down on a Jquery Login Page

I am currently utilizing JQuery version 1.4.2. My objective is to allow the user to scroll down to the login form by clicking on the 'login' option in the top menu, similar to Twitter's design. The implementation works impeccably with insert ...

Tips for sending API requests as an object rather than an array

Hello there! I'm having trouble posting the data as an object. I attempted to adjust the server code, but it keeps throwing errors. Here is a snippet of the server code: const projectData = []; /* Other server setup code */ // GET route app.get(&a ...

React Virtualized - Blank screen issue occurring because of continuous scrolling through a large list of ITSM items

I am currently working on a lengthy list of items and utilizing the react-virtualized library for this purpose. However, I have encountered an issue that needs addressing. https://i.stack.imgur.com/ROdjp.gif Upon attempting to scroll down for 2 seconds, ...

Executing an AJAX request with a specific state in Node.js

Instead of rendering add.jade directly, I have chosen to enhance the user experience by making an AJAX call to the endpoint. This allows me to maintain the URL as localhost:3000/books, rather than localhost:3000/books/add which lacks navigation state for ...

Obtain the current name of the Material UI breakpoint

Looking for a MUI function called MaterialUIGiveMeCurrentBreakPointName that can help me execute an action in a component like so: const currentBreakPointName = MaterialUIGiveMeCurrentBreakPointName() if(currentBreakPointName === 'myCustomBreakPointN ...

Adjust camera to align with the loaded object in Three.js

I've been struggling to center the camera around the model loaded through the stl loader. I've tried adjusting variables and setting different positions for the mesh and camera, but the camera still isn't centered on the design! Here's ...

Single-select components in React Native

I am currently working on implementing a simple single selectable item feature, illustrated in the image provided below. https://i.stack.imgur.com/U2rJd.png At this moment, I have set up an array containing my data items and utilized the .map function to ...

Send a bundle of data through AJAX requests

An issue has been encountered on an HTML/PHP page named sucessful.php where a variable job_id passed from another page is not being received by the destination page interview.php. The problem arises when attempting to transfer two variables and their corr ...

Retrieving the value of an inputtext component in PrimeFaces using jQuery

I have the following code snippet: <h:form id="calcForm"> <h:panelGrid columns="3" styleClass="panelGridNoBorder"> <p:inputText id="value" /> <p:commandButton value="calculate " onclick="calc()" /> </h:panelGrid> ...

Transform the jQuery each method into vanilla JavaScript

I have a script that displays a dropdown in a select box. The current script I am using is as follows: jQuery.each( dslr, function( index, dslrgp) { var aslrp= dslrgp.aslrp; jQuery.each( aslrp, function(index2, pslrp) { var found = 0; ...

Exchanging the positions of two elements within a 2D array

I am having trouble sorting the 2-dimensional array based on column values. Here is the array: var cl12 = [[9, 10.5], [10, 11.5], [12, 13.5], [12.5, 14.5], [14.5, 15], [16, 18], [16, 17]] In the above array, you can see that the last two elements have th ...

The program is unable to locate the script files I have saved

Currently, I am utilizing node.js alongside express and AngularJS to construct my website. However, I am encountering an issue in the console output where the program seems unable to locate my script files. GET http://localhost:3000/images/entet_gauche.gi ...

You can use the following code to retrieve the value of the checked radio button with the name "nameOfradio", but make sure that the radio button

I am attempting to extract the value from a radio button using the following code: document.querySelector('input[name=nameOfradio]:checked').value; However, I would also like to retrieve a value even if none of the radio buttons are checked. Fo ...

``Can you provide step-by-step instructions on how to delete a particular item from

Currently, I am in the process of developing a simple comment list application using JavaScript and local storage. I have completed all the necessary details, but now I need to figure out how to remove a specific item that was created by the application ...

Tips for managing JavaScript within PHP code and vice versa

Usually, I include a lot of JavaScript on the website. However, the client informed me that they were unable to view the website correctly as they had disabled JavaScript on their end. After investigating, I discovered this issue. Now, my dilemma is wh ...