...

View Full Version : passing an object to a function



tater
06-23-2004, 05:31 PM
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 :confused: i need a reference to the name of the second select field

Thanks

in function
function makeFirstOptionNull( :confused: )
{
document.someForm. :confused: .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 :confused: ?>)"></select>
<select name="<? echo $secondSelect ?>"></select>

homerUK
06-23-2004, 07:03 PM
could you post your code inside the [code] tags? the part in question is being shown as a smiley!! :confused:

tater
06-23-2004, 09:00 PM
basically why doesn't the following work
in function

function makeFirstOptionNull( selObj )
{
document.someForm.selObj.options.value = null; //doesn't work
document.someform.submit();
}
in body

<? $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;

tater
06-23-2004, 09:19 PM
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;

Roy Sinclair
06-23-2004, 09:54 PM
Two words: Bracket Notation.

The Bracket Notation method will take care of you:



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.

tater
06-23-2004, 10:15 PM
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..


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

glenngv
06-24-2004, 04:48 AM
Since you already pass the select object reference to the function, you can use it directly without going through the form object again.


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum