Codebunnie
Programmer
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!
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!