Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can autocapitalize be turned off with javascript in mobile safari?

Mobile safari supports an attribute on input elements called autocapitalize [documented here], which when set to 'off' will stop the iPhone capitalizing the text input into that field, which is useful for url or email fields.

<input type="text" class="email" autocapitalize="off" />

But this attribute is not valid in html 5 (or another spec as far as I know) so including it in the html will produce an invalid html page, what I would like to do is be able to add this attribute to particular fields onload with javascript with something like this:

$(document).ready(function(){
  jQuery('input.email, input.url').attr('autocapitalize', 'off');
});

which adds the correct attribute in firefox and desktop safari, but doesn't seem to do anything in mobile safari, why?

like image 668
Andrew Nesbitt Avatar asked Jul 17 '09 22:07

Andrew Nesbitt


3 Answers

This should be fixed in iPhone OS 3.0. What version of iPhone OS are you trying this on?

Email: <input id="email" type="text"><br>
URL: <input id="url" type="text"><br>
<script>
//document.getElementById("email").autocapitalize = 'off';
//document.getElementById("url").autocapitalize = 'on';
document.getElementById("email").setAttribute('autocapitalize', 'off');
document.getElementById("url").setAttribute('autocapitalize', 'on');
alert(document.body.innerHTML);
</script>
like image 131
ddkilzer Avatar answered Oct 19 '22 19:10

ddkilzer


Side note. You can improve the user experience on iOS even more by specifying the type of the input to be "email" to automatically bring up the "email" keyboard (slightly better characters for typing an email).

<input type="email" class="email" autocapitalize="off" />

Here is some documentation on how input types can control the iOS keyboard.

like image 8
Joseph Pecoraro Avatar answered Oct 19 '22 20:10

Joseph Pecoraro


It's just as invalid if you add it via script or if you add it in the markup. It's just that the validator isn't able to notice it if you add it via script.

Just put it in the markup and put a comment next to it, like <!-- the "autocapitalize" attribute is an Apple proprietary extension for the iPhone to change its IME behaviour -->, that way people who look at the code in the validator will know what's up.

like image 4
Ian Hickson Avatar answered Oct 19 '22 19:10

Ian Hickson