Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(Noob Help) Passing/Reading Arrays 1

Status
Not open for further replies.

Codebunnie

Programmer
Mar 23, 2006
13
US
Hey all....
I am new to JavaScript, so bare with me on this. Im all googled out trying to figure out what is going on with this code.

I am trying to pass an array to a function and then output its contents, but I keep getting the term [object] or Undefined when the returned array is outputted.

Essentially, I want to take an array of input, and pass that info to another function for processing.

The code is for a form with checkboxes that a user searches content with. Each box represents a field to search on. All boxes are checked onLoad (requirement by client). The user then unchecks what they dont need. Once the form is submitted, I want to grab the checked boxes that they used, and redisplay them, so that the user knows what fields they searched on.

heres the code:

//remember all boxes are checked by default
var boxVal = new Array()

function findBoxes(){
var numOfBoxes = ""

numOfBoxes = document.forms.SearchQuery.Field.length //how many checkboxes do we have?
alert("Num of total Boxes is " + numOfBoxes)

for (i = 0; i < numOfBoxes; i++) { [highlight] //keep counting to get all the boxes [/highlight]
if( document.forms.SearchQuery.Field.checked) { [highlight]//but only find the boxes that are checked [/highlight]
boxVal= document.forms.SearchQuery.Field.value + " " [highlight] //set boxVal to the val of the checked box it found [/highlight]
alert("Checked Boxes are " + boxVal) [highlight] //Output the values to ensure that it works (which it does)[/highlight]
} }
return boxVal
}



function checkedBoxes(boxVal) {//
[highlight]//boxVal == Array of selected fields returned from findBoxes()[/highlight]
alert(boxVal) [highlight]//Output I get for this box is 'Undefined'[/highlight]
alert("value = " + document.forms.SearchQuery.Field.value) [highlight] //Output I get is 'Undefined'[/highlight]

[highlight]//if the values of the boxes passed from findBoxes() matches the values on the form
//set only those boxes to checked, and uncheck the rest (ones that do not match)[/highlight]

if (boxVal == document.forms.SearchQuery.Field.value)
{ document.forms.SearchQuery.Field.checked = true }
else
{ document.forms.SearchQuery.Field.checked = false } //end loop

} //End Function

[highlight]//findBoxes() is run onClick, checkedBoxes() runs onSubmit
//when checkedBoxes is called, the parameter passed is checkBoxes(this).[/highlight]

Any suggestions are greatly appreciated. Also, any suggestions on a different approach for this problem are welcome!
 
'sorry. Wasn't paying close enough attention.

Let's say you have 20 boxes on your page. boxArray.length might be 20, then. Let's say only two were checked: checkbox #3 and #20.

thirdSplit[0] = 3; boxArray[0] = 1; //if 1-based
thirdSplit[1] = 20; boxArray[1] = 2;

If you want to check 3 and 20, then do this instead:

Code:
for(var i=0; i<thirdSplit.length; i++)
 boxArray[thirdSplit[i]].checked = true;

If I understand your code correctly, the VALUE held at the specified INDEX in thirdSplit indicates the INDEX in boxArray corresponding to the checkbox to be checked.

Follow?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
oh my goodness, why didnt i see that before! thanx, let me try this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top