-2

I have following html

<div>
   <span>Click me</span>
   <select>
       <option value="1" selected="selected">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
   </select>
</div>

jQuery:

$('span').on('click', function () {
   $('select').focus(); 
});

What I want to do is when I click on span tag the select menu needed to be focused and dropped down without changing the structure. I spent hours to find a solution for this but I couldn't.

http://jsfiddle.net/dqc9nLgk/1/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sam No1
  • 153
  • 13

3 Answers3

2

You have used input as selector in fiddle. it should be select instead:

$('span').on('click', function () {
  $('select').focus(); 
});

Working Demo

Update: For opening dropdown-

var dropdown = $('select')[0];
event = document.createEvent('MouseEvents');
event.initMouseEvent('mousedown', true, true, window);
dropdown.dispatchEvent(event);

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

If it is possible to change markup, you may use label html element instead of span. It is been designed to do what you want (or describing here) without JavaScript.

UPD:

About "dropped down" select, I suggest you to read related question: How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)?

Community
  • 1
  • 1
BlitZ
  • 12,038
  • 3
  • 49
  • 68
0
$('span').on('click', function () {
   $('select').focus(); 
});


http://jsfiddle.net/dqc9nLgk/2/
Navneet
  • 447
  • 4
  • 13