Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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?

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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?

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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.

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    right...

    so does the above work for you, then?

  • Users who have thanked xelawho for this post:

    Scott.Atkinson (09-23-2011)

  • #9
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •