Simulate a click on a select box inside a div when a div is clicked

Quick question. Can I simulate a click on a select box (regardless of id or selectbox class) inside a div using jQuery. There is only one checkbox in the div. thanks in advance

Here is the HTML:

<div class="custom-select">
<select id="operator_select">
<option  value="1">1</option>
<option  value="2">2</option>
<select>
</div>

The jQuery code I tried:

    $(function(){
     $(".custom-select").click(function(){
       $('select',this).click();
       });
     });

here is the fiddle: http://jsfiddle.net/arjdev/5Sch2/

+3
source share
3 answers
$(function () {
    function open(elem) {
        if (document.createEvent) {
            var e = document.createEvent("MouseEvents");
            e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            elem[0].dispatchEvent(e);
        } else if (element.fireEvent) {
            elem[0].fireEvent("onmousedown");
        }
    }

    $(".custom-select").click(function () {
        open($(this).find('select'));
    });
});

Your select tag hasn't ended either </select>.

Fiddle

+2
source

try the launch method

 $(function(){
 $(".custom-select").click(function(){
   $('select').trigger( "click" );
   });
 });
0
source

..

    $(document).ready(function() {
         $('.custom-select').find('select').trigger('click');
    });
0

All Articles