2

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the responses, dumb mistake, but I'm always learning. I'll accept the answer when I'm able to.
0

Because you are passing in the id instead of the this

Comments

0

pass a value as formclear(this) instead of formclear(this.id)

<input type="text" name="email" id="email" value="Requires validation"
onfocus="formclear(this)" onblur="formappear()">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.