Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-23-2011, 06:48 PM   PM User | #1
Scott.Atkinson
New Coder

 
Join Date: Sep 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
Scott.Atkinson is an unknown quantity at this point
Making an Array deal with how ever many fields you decide to pass it

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
Scott.Atkinson is offline   Reply With Quote
Old 09-23-2011, 06:56 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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:

Code:
 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?
xelawho is offline   Reply With Quote
Old 09-23-2011, 06:59 PM   PM User | #3
Scott.Atkinson
New Coder

 
Join Date: Sep 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
Scott.Atkinson is an unknown quantity at this point
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
Scott.Atkinson is offline   Reply With Quote
Old 09-23-2011, 07:12 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
mmm... now we're getting very hypothetical. But in the absence of code, I'm thinking that what you can do is this:

Code:
 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?
xelawho is offline   Reply With Quote
Old 09-23-2011, 07:21 PM   PM User | #5
Scott.Atkinson
New Coder

 
Join Date: Sep 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
Scott.Atkinson is an unknown quantity at this point
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

Code:
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

Last edited by Scott.Atkinson; 09-23-2011 at 07:31 PM..
Scott.Atkinson is offline   Reply With Quote
Old 09-23-2011, 07:32 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
ah. no. in that case I think all you would have to do is

Code:
 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.
xelawho is offline   Reply With Quote
Old 09-23-2011, 07:36 PM   PM User | #7
Scott.Atkinson
New Coder

 
Join Date: Sep 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
Scott.Atkinson is an unknown quantity at this point
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.
Scott.Atkinson is offline   Reply With Quote
Old 09-23-2011, 07:39 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
right...

so does the above work for you, then?
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
Scott.Atkinson (09-23-2011)
Old 09-23-2011, 08:20 PM   PM User | #9
Scott.Atkinson
New Coder

 
Join Date: Sep 2011
Posts: 34
Thanks: 7
Thanked 0 Times in 0 Posts
Scott.Atkinson is an unknown quantity at this point
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.
Scott.Atkinson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:06 AM.


Advertisement
Log in to turn off these ads.