Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a select refresh while it's open when changed - chrome

This works as expected on Firefox but not on Chrome.

Fiddle example: http://jsfiddle.net/8Ab6c/1/

setTimeout(function(){
    $('#s1').append("<option>NEW</option>");
},2000);

If the selectbox is opened when the timeout finishes, the list isn't updated until you close and reopen the selectbox. Is there a way to make it update the select list while it's still open on Chrome? (I guess on IE too idealy, though that's not required)

I can use replaceWith to force it to be deselected but this has an ugly effect and I'd prefer if it just changed the list and kept it open.

The actual situation is that my select box is waiting on an ajax call and it's quite easy for the user to open the select box before the ajax is finished.

like image 446
DanielST Avatar asked Jul 31 '14 17:07

DanielST


People also ask

How do I refresh a page in Chrome?

Chrome and Windows:Hold down Ctrl and click the Reload button. Or Hold down Ctrl and press F5.

What is refresh in Chrome?

Ctrl + F5 is the shortcut to trigger a refresh, which will force the page to reload. To make the browser refresh without relying on the cache, use Shift + Ctrl + F5.

How can I refresh my browser?

In most browsers, pressing Ctrl+F5 will force the browser to retrieve the webpage from the server instead of loading it from the cache. Firefox, Chrome, Opera, and Internet Explorer all send a “Cache-Control: no-cache” command to the server.

How do you stop a page from refreshing?

Disabling Auto Refresh Within the Browser To disable auto refresh within the browser, open the Chrome browser, and in the address bar, copy and paste the following: “chrome://discards/”.


1 Answers

There are two pieces to this, you have to blur the drop-down when you update it and then reopen it. Taking away focus is easy, reopening the list is not. I used this technique https://stackoverflow.com/a/10136523/436036 to reopen it.

var showDropdown = function (element) {
    var event;
    event = document.createEvent('MouseEvents');
    event.initMouseEvent('mousedown', true, true, window);
    element.dispatchEvent(event);
};

setTimeout(function(){
    var focused = false;
    if($('#s1').is(":focus")) {
        $('#s1').blur();
        focused = true;
    }
    $('#s1').append("<option>NEW</option>");
    if(focused) {
        showDropdown($('#s1')[0]);
    }
},4000);

Here is the fiddle: http://jsfiddle.net/8Ab6c/2/

like image 65
Fireandlight27 Avatar answered Oct 23 '22 02:10

Fireandlight27