I'm having a problem with a basic addevent() javascript implementation. I basically want to find all <input> tags and add 'onfocus' events to them that will clear their 'value' attributes. I created a test page that gives me an "inputs has no properties" error when it gets to the addevent function, but I can access the properties of 'inputs' with any other command. What am I missing here?
Test page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html>
<head>
<script type="text/javascript">
function addEvent(obj, evType, fn, useCapture){
if (obj.addEventListener){
obj.addEventListener(evType, fn, useCapture);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be attached");
}
}
function addfocusclear () {
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
alert(inputs.value);
addEvent(inputs, "focus", function() {inputs.value = '';}, false);
}
}
</script>
</head>
<body onload="addfocusclear();">
<input value="sampletext1" type="text" />
<input value="sampletext2" type="text" />
</body>
</html>
Thanks, Erik
Test page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html>
<head>
<script type="text/javascript">
function addEvent(obj, evType, fn, useCapture){
if (obj.addEventListener){
obj.addEventListener(evType, fn, useCapture);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be attached");
}
}
function addfocusclear () {
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
alert(inputs.value);
addEvent(inputs, "focus", function() {inputs.value = '';}, false);
}
}
</script>
</head>
<body onload="addfocusclear();">
<input value="sampletext1" type="text" />
<input value="sampletext2" type="text" />
</body>
</html>
Thanks, Erik