jQuery "TypeError: invalid 'in' operand a"

33,008

Solution 1

You are iterating a string, you don't need two .each() functions

$.each(data.fields, function (i, field) {
    $('[name="'+field+'"]').addClass('form_err');
    console.log(field);
});

Solution 2

Remember to add dataType: 'json'; so that it can be looped as an array and not a string.

Share:
33,008
Mariano
Author by

Mariano

Updated on July 10, 2022

Comments

  • Mariano
    Mariano almost 2 years

    I have the following json array returning from an ajax call:

    {"err":"err_type","fields":["field1","field2"]}

    when tryin to print it out with this function:

    $.each(data.fields, function (i, field) {
        console.log(field);
        $.each(field, function (j, f) {
            $('[name="'+f+'"]').addClass('form_err');
            console.log(f);
        });
    });
    

    i get this:

    data1
    
    TypeError: invalid 'in' operand a
    ...turn function(b){return db(a,b).length>0}}),contains:fb(function(a){return funct...
    

    and so i can't figure out how to use this array! Anyone have any idea?