I have a text input that need an url as value (complete of http://) and I want that if the user don’t writes it himself if gets added automatically. My code as follows,
jQuery
$('.txtUrl').keypress(function(e) {
if(e.keyCode == 13) {
var ini = $(this).val().substring(0,3);
if (ini === 'http'){
$.noop()
}
else {
// get value from field
var cur_val = $(this).val();
// do with cur_val
$(this).val('http://' + cur_val);
}
}
});
HTML
<input type="text" class="txtUrl" />
You’re comparing
httpto the three first characters (substring(0,3)) of the text which, of course, never will be true. Change it to: