...

View Full Version : Making an Array deal with how ever many fields you decide to pass it



Scott.Atkinson
09-23-2011, 07:48 PM
Hello,

Im hoping someone can help me,

Im trying to pass fields to a javascript function that will check the value of each field and it its empty then it will turn the field color red which is what its doing at the moment, but i want to be able to use this function globaly through out my project, so is there a way of not saying how many fields go in to that array, it will just deal with the amount of fields it gets passed?

Thanks

xelawho
09-23-2011, 07:56 PM
if I understand the question correctly, you push your objects onto the array (lets call it fields) and then manipulate them with a loop like this:



for (var i = 0; i < fields.length; i++) {
if (fields[i].value == "") {
fields[i].style.backgroundColor ="red"
}
}


putting the length in the loop counter makes sure all array objects are checked, regardless of how many there are.

or did I misunderstand?

Scott.Atkinson
09-23-2011, 07:59 PM
Nope thats exactly what i ment thanks :) one other question though if i may, say i pass 6 fields to the function and one of them fields are to be sent on to another function once the previous 5 fields have been checked and there not null how can i specify the relevant field to pass on, would i use document.getElementById('FieldName').value;

If
check fields for null values
Else
Pass the relevant field to other the function

xelawho
09-23-2011, 08:12 PM
mmm... now we're getting very hypothetical. But in the absence of code, I'm thinking that what you can do is this:



for (var i = 0; i < fields.length; i++) {
if (fields[i].value == "") {
fields[i].style.backgroundColor ="red"
} else {
myFunction (fields[i]);
}
}

function myFunction (thefield) {
//do whatever you want with the field as long as you refer to it as thefield
}


or have you lost me?

Scott.Atkinson
09-23-2011, 08:21 PM
mmm, i can see what your doing with your example, but say i had a field called JustAnExample and that was within the 6 fields i passed, i think it would get a bit tricky trying to do get the relevant field out of the selection i sent it, would this work




Function AuditFields(Fields)
{
for (var i = 0; i < fields.length; i++) {
if (fields[i].value == "") {
fields[i].style.backgroundColor ="red"
} else {
var RelevantField;
RelevantField = document.getElementById('JustAnExample').value;
myFunction(RelevantField)
}
}
}

function myFunction (thefield) {
//do whatever you want with the field as long as you refer to it as thefield
}




Not sure if that correct but thats what i kind of had in mind?

And the function parameter Fields do i have to specify its an array of will the for loop realise? iv only just started javascript so please bare with me :(

xelawho
09-23-2011, 08:32 PM
ah. no. in that case I think all you would have to do is



for (var i = 0; i < fields.length; i++) {
if (fields[i].value == "") {
fields[i].style.backgroundColor ="red"
} else if (fields[i].id == "JustAnExample"){
myFunction (fields[i]);
}
}

function myFunction (thefield) {
//do whatever you want with the field as long as you refer to it as thefield
}

supposing that JustAnExample could not have an empty value - if it could, you would want to get rid of the else.

Scott.Atkinson
09-23-2011, 08:36 PM
Well the field im passing isnt going to be null its an ID for a Blanket, which will pop up and the user will be able to select values from 2 drop down boxes which are populated with information, but before they see the popup i need to check to make sure they have put information in the fields first.

and if they have then they can carry on.

xelawho
09-23-2011, 08:39 PM
right...

so does the above work for you, then?

Scott.Atkinson
09-23-2011, 09:20 PM
Ill be testing it tomorrow as im not at work at the moment, and this site is blocked from work....Useless i know

but from what we have spoke about and the code snippets you have shown it seems straight forward so hopefully fingers crossed it works :) Thanks for your help and knowledge.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum