17

Does anyone know if it is possible to open a select programmatically in angularjs. Ive tried

angular.element(el).trigger('focus');
angular.element(el).trigger('click');
angular.element(el).trigger('mousedown');

Nothing works.

I also tried

$scope.doSomething = function(){
    setTimeout(function() {
        var el = document.getElementById('test');
        var e = document.createEvent("MouseEvents");
        e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        worked = el.dispatchEvent(e);
    }, 0);

}

The above set focus but do not open the select.

Is it possible?

Tim
  • 41,901
  • 18
  • 127
  • 145
user727507
  • 275
  • 1
  • 2
  • 11

6 Answers6

1

Try this for Hover:

JS:

$(document).ready(function(){
$("#selectId").hover(function(){
   $(this)[0].size=$(this).find("option").length;
});
$("#selectId").click(function(){
   $(this)[0].size=0;
});
});

HTML:

<select id="selectId">
  <option>Volvo</option>
  <option >Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
1

You can achieve this by setting the size of the select and having it float using position:absolute.

<div  style="position:relative">.<select style="position:absolute;top:-8px;left:-20px" onClick="onAssign(this)">   
    <option>a</option>
    <option>b</option>
    <option>c</option>
</select></div>

function openMySelect(){
  document.querySelector(‘#mySelect’),size=YOUR_OPTIONS.length;
}

function onAssign(data,select){
  select.size=1
  // your logic
}
Frazer Kirkman
  • 1,003
  • 1
  • 14
  • 23
0

I think I found a solution, at least works for me:

First, try to get the arrow of the ng-select and then force a mousedown event

let arrowSpan = document.querySelector('.ng-arrow-wrapper');
arrowSpan.dispatchEvent(new CustomEvent('mousedown', { bubbles: true }))

Select span arrow

-1

try this

function openSelect(selectId) {
    var event = new MouseEvent('mousedown', {
        'view': window,
        'bubbles': true,
        'cancelable': true
    });
    var cb = document.getElementById(selectId);
    cb.size=4;
    cb.dispatchEvent(event);
}
openSelect("testId");

or read more here Triggering built-in events

haste
  • 1
  • 3
-1

The answer provided by haste doesn't work:

var cb = element[0].getElementsByClassName('some-selector')[0];
var event = new MouseEvent('mousedown', {
    'view': window,
    'bubbles': true,
    'cancelable': true
});                        
cb.dispatchEvent(event);

I use more simple method:

var cb = element[0].getElementsByClassName('some-selector')[0];
angular.element(cb).triggerHandler('click');
Community
  • 1
  • 1
mirik
  • 356
  • 4
  • 18
-8

i had same problem and finally i'm create my own directive:

angular.module('openselect', [])
    .directive('openselect', function () {
        var showDropdown = function (element) {
            var event;
            event = document.createEvent('MouseEvents');
            event.initMouseEvent('mousedown', true, true, window);
            element.dispatchEvent(event);
        };
        return {
            restrict: 'A',
            scope: {
                'elemento': '@'
            },
            link: function (scope, elem, attrs, ctrl) {
                elem.bind('click', function () {
                    var elemento = document.getElementById(scope.elemento);
                    showDropdown(elemento);
                });
            }
        };
    });

To use:

<div style="position:relative">
<span ng-show="submittedForm.contry.$error.required && co_form.contry.$error.required" class="label label-danger">Indicanos tu país</span>
<select name="country" class="form-control input-md" ng-model="formCheckout.country" id="myCountry" placeholder="España" required>
    <option value="6" selected="selected">España</option>
    <option value="1">Alemania</option>
    <option value="15">Portugal</option>
</select>
<span class="glyphicon glyphicon-chevron-down" style="position: absolute; right: 10px; color: #A6A6A6; top: 18px;" openselect elemento="myCountry"></span>

Adds directive to any tag passing id of select (elemento) that you want to open.

You can see the very basic javscript code inside directive if you want to use in other context;

hope it helps ;D