Created
June 6, 2014 01:39
-
-
Save cvan/117bc1f88e4dfca6dba7 to your computer and use it in GitHub Desktop.
prefill `input[type=url]` field with "http://" upon focus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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