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 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing an object to a function

    in the code below $firstSelect and $secondSelect contain the names of select fields in the form someForm. I am needing to use the name of the second select field in the function. where there is a i need a reference to the name of the second select field

    Thanks

    in function
    function makeFirstOptionNull( )
    {
    document.someForm. .options.value = null;
    document.someform.submit();
    }

    in body
    <form name="someForm" method="post" action="itself.php">
    <input type="hidden" name="itemCount" value="
    <? if ($_SERVER['REQUEST_METHOD'] == 'GET')
    {
    $itemCount = 1;
    echo $itemCount;
    }
    else
    echo $_POST['itemCount'];
    ?>">
    <? if ($_SERVER['REQUEST_METHOD'] == 'GET')
    $itemCount = 1;
    else
    $itemCount = $_POST['itemCount'];

    for ($lineItem = 1; $lineItem <= $itemCount; $lineItem++)
    {
    $firstSelect = "firstSelect".$lineItem;
    $secondSelect = "secondSelect".$lineItem; ?>

    <select name="<? echo $firstSelect ?>" onChange="makeFirstOptionNull(<? echo ?>)"></select>
    <select name="<? echo $secondSelect ?>"></select>

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    could you post your code inside the [code] tags? the part in question is being shown as a smiley!!

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    basically why doesn't the following work
    in function
    Code:
    function makeFirstOptionNull( selObj )
    {
    	document.someForm.selObj.options.value = null; //doesn't work
    	document.someform.submit();
    }
    in body
    Code:
    <? $lineItem = 1
        $firstSelect = "firstSelect".$lineItem;
        $secondSelect = "secondSelect".$lineItem; ?>
    
       <select name="<? echo $firstSelect ?>" onChange="makeFirstOptionNull(<? echo $secondSelect ?>)"></select>
       <select name="<? echo $secondSelect ?>"></select>
    this is an example that doesn't work. what doesn't make sense to me is that when i do a document.writeln(selObj); instead of what is in the function. the output is

    [object]

    if I change to document.writeln(selObj.name); it displays

    secondSelect1

    In the function i want the equivalent of
    document.someForm.secondSelect1.options.value = null;

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ive looked at it some more and i know what im unable to do

    say i have a variable
    var junk= "fieldname";

    how do i execute

    document.someform.fieldname.options.value = null;

    using junk

    this doesn't work
    document.someform.junk.optioins.value = null;

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Two words: Bracket Notation.

    The Bracket Notation method will take care of you:

    Code:
    var junk = "fieldname';
    
    document.forms.formname.elements[junk].options.value = null;
    
    or
    
    document.forms["formname"].elements[junk].options.value = null;
    Please note that you can leave out the ".forms" part of the first reference above but it's more correct to properly direct the lookup to the forms array and in a tight loop that's executed a lot of times there will be an appreciable performance difference.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you

    that was what i needed to know

    why does it pass a varible to a function as an object
    for instance in a function i had to use the .name
    example..

    Code:
    function makeFirstOptionNull( selObj )
    {
    	document.someForm.elements[selObj.name].options.value = null; 
    	document.someForm.submit();
    }
    the code above worked but i still don't understand why the passed variable is an object

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Since you already pass the select object reference to the function, you can use it directly without going through the form object again.
    Code:
    function makeFirstOptionNull( selObj )
    {
    	selObj.options.value = null; 
    	selObj.form.submit();
    }
    Every form control such as textbox, checkbox, select and the like has form property that refers the form tag they belong to. So you can get the form reference by doing selObj.form.


  •  

    Posting Permissions

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