Sparky
Sparky

Reputation: 883

Values stored in array is undefined

I'm trying to save the ids of input fields that were left blank in an html form using an array called emptyFields like this

var emptyFields = [];
    $("input").each(function(){
        if( $(this).val() === ''){
            emptyFields.push($(this).attr('id'));                       
    } 

But when I try to use this array (see below) to access the empty fields, I'm getting an undefined warning in Chrome's debugging. Where did I go wrong?

for(var i = 0; i < emptyFields.length; i++){
            $("input").attr(emptyFields[i]).val("?");
            // do stuff
}

Upvotes: 0

Views: 22

Answers (1)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

you already pushed an array of ids so you can select the input with its ID

for(var i = 0; i < emptyFields.length; i++){
            $("input#"+emptyFields[i]).val("?");
            // do stuff
}

Upvotes: 1

Related Questions