Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How to create text input and select combo

I want to have select element so that my users can choose from a list of items, or if they like, type a new value that doesn't exist.

I know I can do a select like:

<select name="myDropdown" id="myDropdown">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

But you can't type into this select. So the next option I found is to use a dataset like:

<input type="text" list="existing-list" id='free-hand-enabled-dropdown'/>
<datalist id="existing-list">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</datalist>

This is much closer, but the text attribute for the option does not display when chosen, only the value is displayed. As a matter of fact, when you inspect this element, the text has been wiped on page load. And the value and text appear together in the dropdown list. I can see how this would work if you just wanted to suggest some items to save the user from typing.

I just want a select element that allows the user to pick from the list as usual, or, type into the input box instead. When the submit button is pressed, javascript will submit the text and value attributes. If the value attribute is blank, I will know that it is a new entry. Open to suggestions if this is an awful idea!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is the jquery solution.

I replaced the text to title attribute, because custom attribute would easily to get.

function submit(){
   const input =  $("#free-hand-enabled-dropdown").val();
  
  //check if the attribute value and label exist
  if(typeof $("#existing-list option[value='" + input + "']").attr("value") &&
     typeof $("#existing-list option[value='" + input + "']").attr("label") == 'undefined'){
    console.log(input);
  }else{
    console.log($("#existing-list option[value='" + input + "']").attr("value"),$("#existing-list option[value='" + input + "']").attr("label"));
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" list="existing-list" id='free-hand-enabled-dropdown'/>
<datalist id="existing-list">
    <option value="1" label="One"></option>
    <option value="2" label="Two"></option>
    <option value="3" label="Three"></option>
</datalist>

<button type="button" onclick="submit()">Submit</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!