Reputation: 195
I have an email box in which I like to show a default value with "enter your email"
Herefore I created an email box in HTML:
<p> Email: <br /> <input type="email" size="30"/> </p>
An the following jQuery:
$(document).ready(function(){
var default = "Please enter your email adress;
$("input").attr("value", default);
});
It does not show up however. Anybody suggestions what I'm doing wrong?
Upvotes: 1
Views: 2432
Reputation: 797
You can also use jquery water mark:
$('#myElement').watermark('Enter your email');
Please see jsfiddler: http://jsfiddle.net/NMtfK/
Upvotes: 3
Reputation: 15490
why are you doing this, you can directly use placeholder
attribute.
there is no need of using jquery or javascript
<p> Email: <br /> <input type="email" size="30" placeholder="please enter your email address"/> </p>
Upvotes: 7
Reputation: 128791
Two things: Firstly, default
is a reserved word in JavaScript so may not be used as a variable name. Change this to something else (e.g. defaultString
):
var defaultString = ...;
Secondly you're missing the closing "
on your assignment string:
var defaultString = "Please enter your email adress;
^
Also you should use val()
for values, not attr()
:
$("input").val(defaultString);
Upvotes: 3
Reputation: 74420
Don't use default
as variable name: SEE WHY?
$(document).ready(function(){
var sdefault = "Please enter your email adress";
$("input").attr("value", sdefault);
});
But you should use instead:
$("input").val(sdefault);
Or better use placeholder attribute on modern browsers.
Upvotes: 2
Reputation: 8975
Use placeholder attribute
<input type="email" placeholder="Enter your email address"/>
Note: The approach you are following is not good because when you will focus on input box then you have to clear all the default text. So use placeholder which is ment for same purpose.
Upvotes: 0