I have an input field using html5 placeholder:
<input type="text" name="website" class="url" required placeholder="enter website">
Using jQuery or direct javascript, I would like to add a string to the data entered by the user in focus. If the field contains more data than the specified string variable (for example: http://example.com ), then the field satisfies my requirements. If it contains only the source string ('http: //'), clear the input value and show the placeholder.
The following code works for me.
var input = $("#processor .url"); var prefix = 'http://'; input.focus(function() { if (input.val().indexOf(prefix) == -1) { input.val(prefix + input.val()); } }).blur(function() { if (input.val() == prefix) { input.val(''); } });
Is there a better way to write this for performance, etc., is this my real question?
source share