21

Safari 10.0 in macOS Sierra seems to have changed the way the placeholder in an input behaves when the input value is changed via JavaScript. It now also differs from what Chrome (53.0.2785.116) is doing.

Until now, when setting an input value via JavaScript the placeholder would disappear. After setting the value back to empty via JavaScript, the placeholder would reappear.

Now, setting the input value via JavaScript does not hide the placeholder, until the input gains focus afterwards (e.g. by being clicked on).

Check this JS Bin for a demo: https://jsbin.com/rogoludahu/edit?html,js,output

Is this the intended behavior? If so, is there a clever workaround for hiding/unhiding the placeholder after a change via JavaScript?

Edit: This has now been filed at rdar://28412751 for Safari 10 and the Safari Technology Preview.

Joshua
  • 1,349
  • 15
  • 26
  • 2
    I noticed the same behaviour! To me, this seems to be a bug – Andrea Marcolin Sep 27 '16 at 09:09
  • 4
    Most likely a bug. Rather hacky way to fix is to set the value twice as a temporary solution https://jsbin.com/qiloyuxura/1/edit?html,js,output – Tigran Petrossian Oct 14 '16 at 06:26
  • try this maybe: document.getElementById("myInput").setAttribute('value', "New Value"); – Dalin Huang Mar 23 '17 at 20:57
  • @DalinHuang: Nice idea, but no change for me - feel free to try in the JS Bin. – Joshua Mar 26 '17 at 09:11
  • The placeholder seems to be working correctly in Safari 10.1 on OSX 10.12.4. Still appears broken (seeing the overlapping placeholder) in iOS Safari on iOS 10.3.1. – Paul May 16 '17 at 04:32
  • @Paul: I can't seem to confirm your experience in macOS 10.12.4 with Safari 10.1 (Build 12603.1.30.0.34). My JS Bin above still shows the incorrect behavior, same with the current Safari Technology Preview. Would you mind checking again with the JS Bin? – Joshua May 17 '17 at 19:35
  • 1
    Where is rdar://28412751? – chipit24 Jun 29 '17 at 18:09

4 Answers4

5

I ran into this problem earlier today. As far as I've seen, there is no good solution. My hacky workaround was to call .focus() and then .blur() on the element as soon as the value was given to it.

2

This has been resolved in Safari Technology Preview Release 37 (Safari 11.1, WebKit 12605.1.2) as well as in regular Safari Version 11.0.3 (WebKit 13604.5.6).

Joshua
  • 1,349
  • 15
  • 26
0

According to this Apple thread (and confirmed by one of my coworkers), this is only an issue if you've opened the Javascript console. So, you probably don't need to worry about a workaround!

Ben Kuhn
  • 1,059
  • 2
  • 10
  • 25
0

I don't know if the bug returned or was never fixed properly (Safari 11.0.3). Can be reproducible with the jsbin from Joshua. The solution for us was to redraw the field on change. We are using ExtJS so this is a snipped for the field override, but should be able to apply it in any other framework or vanila.

if(Ext.isSafari && !Ext.isEmpty(me.emptyText)) {
    me.on('change', function() {
        var me = this;

        if(me.rendered && me.el && Ext.isFunction(me.el.redraw)) {
            me.el.redraw()
        }
    })  
}