Clear textfield text on focus

I have a text field containing ‘Enter search string…’ for example.

When the text field is focused I want it to remove this text and then on blur to stick it back in (if the user hasn’t entered any of their own text in the field)

Is there a Yii way to do this or do I have to write the JS to do it?

Would be nice to have something similar to ‘prompt’ attribute like in dropdown list box.

Cheers

JS is the Yii way - I believe you can use onBlur. :)

there is a non-javascript way to implement this in HTML5, covered here

i know this won’t work for older browsers, but it would be useful in a near future

to do with javascript, i developed this little function




$(document).ready(function(){$('.clearMeFocus').focus(function()

{if($(this).val()==$(this).attr('title'))

{$(this).val('');}});$('.clearMeFocus').blur(function()

{if($(this).val()=='')

{$(this).val($(this).attr('title'));}});$('#searchform').submit(function(){if($('#search_s').val()==''||$('#search_s').val()==$('#search_s').attr('title')){return false;}});});

</script>



and in the form, the input would be




<input type="text" title="Enter search string" class="clearMeFocus" name="search_s" value="Enter search string" id="search_s">



hope it helps!

:)

PS. Be sure jquery core is registered in the same page

regards