I'm trying to figure out how to select a random item from a json array and show that item in a DIV.
Below is my code. I hope someone can help.
var url = 'path to json';
$.getJSON(url, function(json) {
var featuredRecipe = '';
$.each(json, function(i, item) {
featuredRecipe += '<div class="col-md-12 col-sm-12 img-margin">' + ' <div class="addthis_inline_share_toolbox" data-url="' + item.recipePageURL +'" data-title="' + item.recipeName + '"></div>' + '<a href="' + item.recipePageURL +'">' + '<img class="img-responsive" src="' + item.recipeImageCategoryURL + '">' + '</a>' + '<a href="' + item.recipePageURL +'">' + '<h3 class="recipeSubCategoryImgCaption">' + item.recipeName + '</h3>' + '</a>' + '</div>';
});
$('#featuredRecipe').html(featuredRecipe);
});
I tried using this, but I am not sure what I have to do next if I include it:
var rand = json[Math.floor(Math.random()*json.length)];