Looks OK to me, seems work. One thing I see is that it's overkill for other browsers since simply setting the value is sufficient for them. Also, it no longer detects/checks for the default value. And it looks like it unnecessarily exposes o as a global variable.
Anyways, I was just playing with it and this looks real good to me:
Code:
<script>
$(document).ready(function(){
$("#curl").focus(function(){
if( this.value == this.defaultValue ) {
if($.browser.msie){
var el = this, rng;
el.value = '';
rng = document.selection.createRange();
document.selection.empty();
rng.text = "http://";
setTimeout(function(){
el.blur();
$(el).removeClass('error').next('label').remove();
el.focus();
}, 0);
} else {
$(this).val("http://");
}
}
});
$("#commentForm").validate();
});
</script>
It interacts very well with the validation script.
Using the method for IE in your post would probably be better:
Code:
<script type="text/javascript">
jQuery(document).ready(function($){
$('#curl').focus(function() {
if(this.value === this.defaultValue){
this.value = 'http://';
if($.browser.msie && this.createTextRange){ // IE
var r = this.createTextRange();
r.moveStart('character', this.value.length);
r.select();
}
}
});
$("#commentForm").validate();
});
</script>
Notes: If you want valid - With that DOCTYPE you need the type attribute on the script tag. I'm now using a document ready syntax that can work with jQuery even if it's in noConflict mode. Using $(this).val() for setting the value is more resource intensive than the ordinary javascript this.value = method and gains no advantage here.
Bookmarks