I tried to find the solution for my problem in other post but I couldn't find something similar.

I've this html structure:

```
<p>title abc</p>
<input name="input_a" ... >
<p>title abc</p>
<input name="input_b" ... >
<p>title abc</p>
<input name="input_c" ... >
...

```

Submitting the form I check in Ajax that all the fields are correctly filled out, if everything is fine it will return 'success' otherwise it will return an array containing the index of the not validated fields.

For example if I fill out only 'input\_b', then I will get `rs_prod = Array(0, '', 2, 3, ...)`

What I want to do now is simply to color in red the `<p>title abc</p>` related to these values, adding a class.

So, I compare each value `rs_prod[index]` with `index` :

```
$.ajax({
...
success: function(rs_prod) {

    if (rs_prod == 'success') {
        // success case
    } else {
        $( rs_prod ).each(function( index ) {
           if (index == rs_prod[index]) {
               $("#add-form p:eq("+index+")").addClass("redText");
           } else {
               console.log(index); // never returns 0
               $("#add-form p:eq("+index+")").removeClass("redText");
           }
       });
    }
}

```

This is working, in fact for all the wrong fields I'm getting:

```
<p class= "redText">title abc</p>

```

At this point, when I'm going to fill the fields that I previously forgot and submit again, I should get the class 'redText' to be removed: and so it happens... it does for all except the first `<p>title abc</p>` that keep the class 'redText' and I can't really understand why.

The only thing I noticed is that `console.log(index)`, in the code above, returns always the value of the index except when that value should be 0. It seems that somehow index loose the value 0.

EDIT/SOLUTION:

I think the problem was mainly due the 0 that for some reason was not considered as value but as false.

This was the array used in ajax to store the fields to check:

```
$fields_req = array(
    0 => 'input1', 
    1 => 'input2',
    2 => 'input3',
    3 => 'input4',
    ...)

```

that I changed in

```
$fields_req = array(
    1 => 'input1', 
    2 => 'input2',
    3 => 'input3',
    4 => 'input4',
    ...)

```

Then as suggested by @Thomas I replaced 

```
if (index == rs_prod[index]) { 

```

with

```
if (rs_prod[index]) {

```

and now it works

LAST EDIT: This is my final solution:

```
if (rs_prod == 'success') {
    // success case
} else {
   $("#add-form p").removeClass("redText");

   $( rs_prod ).each(function( index ) {
     if (rs_prod[index]) {
         $("#add-form p:eq("+index+")").addClass("redText");
     } 
});

```