0

I want to show selectbox option using when the page loads. I have used .simulate() and .trigger function but it's not working, so can you please suggest some solution for that.

Code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="simulate.js"></script>
<script>
$(document).ready(function(){
    $("#test").simulate('mousedown'); // not working
    $("#test").trigger('click'); // not working
    //$("#test").attr('size',3); // size is not as i need. i want to display option as any dropdown shows.
});
</script>

<select id="test">
   <option value="0">select option</option>
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
</select>

Please help me to out from this problem...

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
Jalpesh Patel
  • 3,150
  • 10
  • 44
  • 68
  • 2
    Possible duplicate: http://stackoverflow.com/questions/430237/is-it-possible-to-use-js-to-open-an-html-select-to-show-its-option-list – ste2425 Jan 28 '16 at 12:53

3 Answers3

0

It's not possible to open a selectbox with JavaScript. I tried and got disappointed. I finally changed the attribute size for the select box so it appears to be open:

$('#test').attr('size',3);

and then when you're finished

$('#test').attr('size',0);

Hope that helps :)

Julien Vavasseur
  • 3,854
  • 1
  • 20
  • 29
  • Thanks for help but as per i have commented in my code i don't need .attr('size',0) this is not as per i required. Its look like multi select. – Jalpesh Patel Feb 01 '16 at 04:27
  • Oh I missed your inline comment. Do you mind using libraries? https://select2.github.io is a simple jQuery selectbox replacement. I think it will fit for you! –  Feb 02 '16 at 05:22
0

Try this...

$(document).ready(function(){

    var myDropDown=$("#test");
    var length = $('#test> option').length;
    myDropDown.attr('size',length);
    $("#test").click(function(){
        myDropDown.attr('size',0);
    }); 

});
Nofi
  • 2,107
  • 1
  • 15
  • 23
  • Thanks for help but as per i have commented in my code i don't need .attr('size',0) this is not as per i required. Its look like multi select. – Jalpesh Patel Feb 01 '16 at 04:27
  • Ohh, I missed your inline comment...sorry for that... BTW i didn't get the point "Its look like multi select"...If you run this code only single select dropdown is populated right. – Nofi Feb 01 '16 at 06:37
  • yes its only single select but its look like we have provided multiple select option for choose – Jalpesh Patel Feb 01 '16 at 07:11
0

I think it's not possible. There's a lot of jQuery plugins out there to enhance the native select's functionality, like the Selectmenu Widget of jQuery UI which has an "open" method:

http://api.jqueryui.com/selectmenu/#method-open

or for Twitter Bootstrap:

http://silviomoreto.github.io/bootstrap-select/methods/#selectpickershow

just to name some...