Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing object into a function

    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;
    }

    }

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by adrive View Post
    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
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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:
    Code:
    <script type="text/javascript">
    alert(document.getElementById("test").tagName);
    </script>
    
    <span id="test">filler text</span>
    Works:
    Code:
    <span id="test">filler text</span>
    
    <script type="text/javascript">
    alert(document.getElementById("test").tagName);
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •