Issue
Quick JavaScript question. I am creating a simple script where a form field entry disappears/reappears using onfocus
and onblur
.
I'm wondering why the following inline script works:
<div>
<input type="text" name="email" id="email" value="Requires validation"
onfocus="if(this.value == 'Requires validation'){this.value = '';}"
onblur="formappear()">
</div>
and the following script with a parameterized function doesn't:
<head>
<script type='text/javascript'>
function formclear(field) {
if (field.value == "Requires validation") {
field.value = "";
}
}
</script>
</head>
<body>
<div>
<input type="text" name="email" id="email" value="Requires validation"
onfocus="formclear(this.id)" onblur="formappear()">
</div>
</body>
I appreciate any responses in advance, I'm sure it's a simple mistake I'm making.
Solution
Dont pass in this.id
, just pass in this
<input type="text" name="email" id="email" value="Requires validation" onfocus="formclear(this)" onblur="formappear()">
Demo: http://jsfiddle.net/se6wm/
You were passing in the id
of the input, so when you were using field.value
it was actually trying to do email.value
which doesn't exist.
Answered By - tymeJV
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.