6

Iam trying to show the dropdown option when javascript function is called. but eventually iam not succeed on this. need help Here is my code:

<script>
function showState(str){ 
$('#Acntname').trigger('click');

}
</script>
...
<select class="input_panel" id="Acntname" > 
<option value="0">Select</option>
<option value="1">Equity Share Capital</option>
<option value="2">Deprication Fund</option>  </select>

<input type="text" class="input_panel" id="accountname" onkeyup="showState(this.value)"/>

i used triggered function to open the dropdown but it doesn't work.

Subodh Bisht
  • 859
  • 7
  • 19
  • 33

4 Answers4

13

Here you go..you can do this by dispatching mousedown event on dropdown..

Demo Fiddle

JS:

window.showState = function (str) {
    var dropdown = document.getElementById('Acntname');
    var event = document.createEvent('MouseEvents');
    event.initMouseEvent('mousedown', true, true, window);
    dropdown.dispatchEvent(event);
}

EDIT: This works perfectly in Chrome <53, not sure for Firefox and IE.

loxx
  • 119
  • 4
Nikhil N
  • 4,507
  • 1
  • 35
  • 53
4

You can do this:

var sel = document.getElementById('Acntname');
var len = sel.options.length;

sel.setAttribute('size', len);

Set the size back to 1 to close it

Peter Munnings
  • 3,309
  • 1
  • 30
  • 43
0

Its not possible to toggle the drop down on triggering other element.

If you want to select the value of select box on keyup event of textbox then try this,

function showState(str){ 
   //  alert(str);
   $('#Acntname option[value="'+str+'"]').prop('selected',true);
}

Demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

You can't open the dropdown list but there is a semi-solution you could do the following

to "open" the list:

$("#Acntname").attr("size","3");

and

$("#Acntname").attr("size","1");

to close the list.

Ricky Stam
  • 2,116
  • 21
  • 25