...

View Full Version : passing object into a function



adrive
11-21-2007, 06:52 AM
hi,

i'm wondering what am i doing wrong as i'm not able to use some of the properties of an object once i passed into the javascript . For example,
I can't access the "name" attribute within the function.

in a html page :

1.) alert(document.frmNote.txtDate.value); //shows 'Hello'
2.) an onclick=validate(document.frmNote.txtData, 'Testing');


in a common.js file :

function validate(objectx, title){

alert("name " + objectx.name);
alert("val " + objectx.value); //this is where i get no actions found for this object

if(objectx.value !=null && length(objectx.value) > 0)
{
return true;
}else{
alert(title + " is empty.");

return false;
}

}

shyam
11-21-2007, 07:03 AM
1.) alert(document.frmNote.txtDate.value); //shows 'Hello'
2.) an onclick=validate(document.frmNote.txtData, 'Testing');


in a common.js file :

function validate(objectx, title){

alert("name " + objectx.name);
alert("val " + objectx.value); //this is where i get no actions found for this object

if(objectx.value !=null && length(objectx.value) > 0)
{
return true;
}else{
alert(title + " is empty.");

return false;
}

}
possible reasons are the typo and most probably because there is no function called length defined in javascript length is usually an attribute of strings or arrays

Trinithis
11-24-2007, 08:48 PM
While I am not sure you are making this mistake, but I often see new coders make the mistake of defining the html element after the script tag containing the code that makes reference to the would-be element.

Does not work:


<script type="text/javascript">
alert(document.getElementById("test").tagName);
</script>

<span id="test">filler text</span>


Works:


<span id="test">filler text</span>

<script type="text/javascript">
alert(document.getElementById("test").tagName);
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum