Bill
Bill

Reputation: 5668

Loop through json object and print results

I have the following json, and I want to loop through it and print out the error messages using jQuery.

Any help would be great. thanks!

var validationErrors = {"errors":["Please enter the month (1-12)","Please enter a day 1-31"]}

Edit, I know I have to use a loop, I'm just not sure really how to separate the error messages so I can print them out separately.

Upvotes: 0

Views: 357

Answers (2)

Tgr
Tgr

Reputation: 28160

$('#error-container').append(
    '<p class="error">'
    + validationErrors.errors.join('</p><p class="error">') 
    + '</p>'
);

Upvotes: 1

js1568
js1568

Reputation: 7032

$(validationErrors.errors).each(function() {
  $('#output').append($('<p>').text(this.toString())); //#output is where you want it printed
});

Upvotes: 0

Related Questions