w3c error: onBlur, onFoucs

hi everyone :slight_smile:

I am trying to valid my site in w3c validtior
and i have 2 errors that i dont know how to fix

the errors:
Line 21, Column 68: there is no attribute “onFocus”
Line 21, Column 94: there is no attribute “onBlur”

the HTML:

<input type="text" name="username" value="enter username..." onFocus="clearText(this);" onBlur="clearText(this);" />

the JAVASCRIPT:

			function clearText(field)
				{
					if (field.defaultValue == field.value) field.value = '';
					else if (field.value == '') field.value = field.defaultValue;
				}

the script clear the value text when blur or foucs and recived him if the input is empty

any suggestions?

Thanks noy.

The validator is telling you that the doctype you’ve chosen does not allow things like
onclick
onfocus
onblur
etc

in the HTML (similarly, you’re probably not allowed to have things like align=“center” either).

The solution to it is to have your Javascript find those inputs itself, and then assign onfocus and onblur event handlers to them.

If multiple events need to be set to these inputs, you’ll want event listeners to make a list of event handlers (since otherwise if you just plug a bunch of handlers on an element, only the last one gets run).

Javascript can either grab the inputs by name or by id.

Let’s run with that suggested solution.

Here’s the input, stripped of it’s awful inline events, inside a form element with an id.


<form id="login">
    <input type="text" name="username" value="enter username..." />
</form>

You will naturally have other content inside the form, but this example works in both cases.

The id attribute of the form, and the name attribute of the field, are what we’ll use to allow our script to attach the events.

The script that you place just before the </body> tag, could look like this:


function clearText(field) {
    if (field.defaultValue == field.value) {
        field.value = '';
    } else if (field.value == '') {
        field.value = field.defaultValue;
    }
}
var form = document.getElementById('login');
form.elements.username.onfocus = function () {
    clearText(this);
};
form.elements.username.onblur = function () {
    clearText(this);
};

With a simple change to the clearText function, where you use the this keyword instead of a passed attribute, you can simplify things even further:


function clearText() {
    var field = this;
    if (field.defaultValue == field.value) {
        field.value = '';
    } else if (field.value == '') {
        field.value = field.defaultValue;
    }
}
var form = document.getElementById('login');
form.elements.username.onfocus = clearText;
form.elements.username.onblur = clearText;

hi thanks alot…
its not work for me and i dont find why…

i run a test to find where is the problem and
var form = document.getElementById(‘login’);
alert(form);

alert show null

Did you place the script at the end of the body, so that it’s capable of attaching those events as the page loads?

If it still doesn’t work for you, then please show us what you’re doing. A test page up on the internet is the best way of showing us.

For reference, here’s a simple test page. Please note, the only reason for the inline script is so that the full code can be posted in one code block. You should ensure that your scripts are in a separate file from your HTML code when using this for real.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<form id="login">
    <input type="text" name="username" value="enter username...">
</form>
<script type="text/javascript">
function clearText() {
    var field = this;
    if (field.defaultValue == field.value) {
        field.value = '';
    } else if (field.value == '') {
        field.value = field.defaultValue;
    }
}
var form = document.getElementById('login');
form.elements.username.onfocus = clearText;
form.elements.username.onblur = clearText;
</script>
</body>
</html>

ohh maybe because i try external file

its not possible to do it with external js file?

An external file makes no difference to how this works.

Where have you loaded your external file? If it’s from the head then you will have trouble. The body doesn’t exist when a head script is being loaded.

The script should be loaded from the end of the body instead.

Not like this:


<head>
    <script type="text/javsacript" src="js/script.js">
</head>
<body>
    ...
</body>

But like this:


<head>
</head>
<body>
    ...
    <script type="text/javsacript" src="js/script.js">
</body>

:slight_smile: works prefect

thanks.

Now that it’s working, I’d like to ask you for some advice.

When I said this earlier:

The script that you place just before the </body> tag, could look like this:

Is there a way that I could have more effectively got the message across?

hmm i think the problem was me. you was absolutely ok…

sorry about it.

It’s okay, we got there.

All the best.

How many browsers did you test that in? If event attachment was indeed this simple I doubt jQuery or prototype.js would have need of event handling sub systems.

Also this attachment approach doesn’t allow for multiple events of the same type on the object which might come up eventually.

its works in FF CHROME IE8

cant test it right now in IE6 IE7

There are always more complicated ways of doing things, such as using addEventListener after DOMContentReady.

The code sample that was provided is the simplest that does the job, allows for an easy understanding of the mechanics involved, and provides a useful path by which more complex requirements can later on be met on an as needed basis.

It’s hard to tell someone who’s at the level of the code originally posted what to do next, because a whole boatload of other things come into play:
-this not working in IE
-the script not running if you stick it in the head without some kind of loader script elsewhere

Usually why when I try to learn how to do something the answer is always “don’t bother learning javascript, just use a library, it does all that stuff for you” : (

and then all the pitfalls said library gets around cross-browser, take me forever to figure out.

The OP is probably going to run into the need for event listeners once more than one function needs to run onevent.

That’s one of the great things about JavaScript. As things grow, there tends to be other more effective ways that can be implemented.