简体   繁体   中英

Select first element of dropdown of an input box

I have an input box

<input id="source" onchange="openDropdown(...)" >

Whenever I enter any text inside this input box a dropdown of suggestion appears. Now I want to automatically select the first suggestion using JavaScript, but can't find any way to do this. REQUIREMENT: You need to only use input id to select the first suggestion. I don't know the onchange function.

NOTE:- I am not having any select element here.I am just having an input element here.So the thing is that i just want to select the first element in the dropdown which will open after we start typing in that input box(& i don't know how this dropdown is coming.I just know that some input box is there.)I am not having the full code.

  $("#source") .change(function () {             
  var abc = $('option:first-child',this) .text();

Please try this .Hope this will help You..

try this code,By default I select option 3 but you can change it

 function openDropdown(){ $("#lst option:first").prop("selected","selected") } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="source" onchange="openDropdown()" > <select id="lst"> <option value=1>option 1</option> <option value=2>option 2</option> <option value=3 selected>option 3</option> <option value=4>option 4</option> </select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM