I always use this snippet in my work:
<input type="text" onblur="if (this.value=='') { this.value='your email'; }" onfocus="if (this.value == 'your email') { this.value=''; }" value="your email" />
Basically it will show a text box with "your email" as the value, when the clicks the text input box - the value becomes blank.. thus they type their email.. if they don't type an email it will reset back to the "your email".
I want to do this or similar with textarea and convert it to jQuery (also the above code in jQuery)?
<textarea name="msg">Message:</textarea><br />
This is how I would do it, hope it helps.
HTML
SCRIPT
You could use data attributes for a generic solution that would apply to textareas and input boxes -
HTML
jQuery
Demo - http://jsfiddle.net/3jwtA/
Use native html attribute
title
to specify the mask text, and then apply following script:html
JS
And in this case you will need only to provide titles to the controls.
Code: http://jsfiddle.net/pJrG9/7/
i think you are referring to Placeholder like this:
http://andrew-jones.com/jquery-placeholder-plugin/
Hope it helps.
This ought to do the trick:
Will work for both inputs and textareas -- Whatever default you set for each will persist. Use as is.
See Fiddle here : http://jsfiddle.net/leifparker/DvqYU/2/
(This pulls and stores the default value in a data attribute)
HTML
JS
EDIT:
An alternative brought up by ANeves, and which makes use of the HTML5 placeholder attribute is below. If you don't care about old browsers, you can use the placeholder HTML on its own (and it works natively, with results similar to the JS above), or otherwise, as below, you'll need to add a JS fallback.
Fiddle here : http://jsfiddle.net/leifparker/DvqYU/14/
HTML
JS