Skip to content

Instantly share code, notes, and snippets.

@cvan
Created June 6, 2014 01:39
Show Gist options
  • Save cvan/117bc1f88e4dfca6dba7 to your computer and use it in GitHub Desktop.
Save cvan/117bc1f88e4dfca6dba7 to your computer and use it in GitHub Desktop.
prefill `input[type=url]` field with "http://" upon focus
// Prefill URL field with 'http://' upon focus.
$('input[type=url][placeholder="http://"]').on('focus', function (e) {
var $this = $(this);
if (!$this.val()) {
// This `setTimeout` is so we set the value *after* the field has
// been focussed; otherwise, the text will be highlighted upon focus.
setTimeout(function() {
$this.val('http://');
}, 0);
}
}).on('blur', function () {
var $this = $(this);
if ($this.val() === 'http://') {
$this.val('').addClass('empty');
if (!this.hasAttribute('required')) {
$this.removeClass('focused');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment