7

I have some json (var data)that looks like this:

{"success":"true","keywords":["firstkeyword","secondkeyword"]}

And im trying to loop through the keywords using this code:

            data.keywords.each(function(e){
                $('#campaign_keywords').append("<p>"+e+"</p>");
            });

But i get the error

Uncaught TypeError: Object firstkeyword,secondkeyword has no method 'each' 

3 Answers 3

13

You need to loop through it like this:

$.each(data.keywords, function (i, v) {
    $('#campaign_keywords').append("<p>"+data.keywords[i]+"</p>");
});

jQuery.each()

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

1 Comment

Just use v to access the values.
3

That's because it's an array and it needs to be a jquery wrapped object in order to use jquery functions. Try:

$.each(data.keywords, function(index, value){...});

Comments

1

Its a javascript array, not jQuery object. just apply $ over it to make it jquery object like

    $(data.keywords).each(function(e){
        $('#campaign_keywords').append("<p>"+e+"</p>");
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.