1

I have this array (JQuery) where I add all my form's controls, it looks like:

var     name       = $("#name"),
        surname    = $("#surname"),
        address    = $("#address"),
        phone      = $("#phone"),
        photo      = $("#photo"),
        grade      = $("#grade"),
        profession = $("#profession"),
        email      = $('#email'),
        title      = $('#title'),

                    allFields =    $([]).add(name)
                                        .add(surname)
                                        .add(address)
                                        .add(phone)
                                        .add(photo)
                                        .add(grade)
                                        .add(profession)
                                        .add(email)
                                        .add(title)
                                        .add(grade);

I want to check the values of each element into the 'allFields' array with

function checkingFieldsArentEmpty(){

              for (var f in allFields){
                    if(f.val() === null)
                        //if any val is null just return false
                        return false;
                }
                return true;
            }

I need ideas in order to improve the last function. Thanks.

3
  • What aspect do you want to improve? Your valid var is unnecessary, just return true at the end. Commented May 29, 2010 at 3:25
  • 1
    Isn't f already an jQuery object? allFields[f].val() seems wrong, shouldn't it just be f.val() ? Commented May 29, 2010 at 3:30
  • Yes, you are right, that's wrong. Commented May 29, 2010 at 3:34

4 Answers 4

2

Try:

var names = ["name", "surname", "address", "phone", "photo",
  "grade", "profession", "title"];
var allFields = {}
$.each(names, function(i, name) {
  allFields[name] = $("#" + name).val();
});

and then you can do:

if (allFields.name) { // true if it has a value, false for ""
  ...
}

The way you're doing it is a little convoluted. Do you want an array or a jQuery object?

Sign up to request clarification or add additional context in comments.

Comments

1

You could replace that function with:

function checkingFieldsArentEmpty(){
    return !(allFields.filter(":input[value]").length != allFields.filter(":input").length);
}

3 Comments

@Felix Guerrero: From the documentation: Selects all input, textarea, select and button elements.
@Felix Guerrero - I don't understand, the function in your post applies to all the elements contained within allFields.
I think that the other Felix thinks that :input only applies to <input> elements.
1

You could use .reduce():

function checkingFieldsArentEmpty(){
    return allFields.reduce(function(prev, curr, index, array) {
        // for checking for emptiness just add  && cur.val() !== "" 
        return prev && curr.val() !== null; 

    }, true);
}

Btw. using jQuery to create and populate the array seems unnecessary to me.

Comments

1

hasEmptyFields = !!$.grep(allFields, function(a) { return a.val() === null; }).length

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.