Using localstorage to apply "likes" to gallery images

$(document).on("click", ".entryImage a" ,function (e) { e.preventDefault(); //prevents anchor tag from working //console.log('success'); console.log($(this).attr("href")); $.ajax({ url: $(this).attr("href"), //grabs link from anchor tag dataType: 'html', success: function (res) { res = $(res).find('#details'); //finds "details" div in src html $('#detail-ajax').html(res); //loads html into #image-details console.log('success'); console.log(res); $.magnificPopup.open({ //opens #popup items: { src: '#popup' }, type: 'inline', mainClass: 'mfp-fade', showCloseBtn: true, callbacks: { syncLikes: syncLikes() } }); } }) }); var checkLikes = function () { $(".likeButton").each(function (i, el) { if (localStorage['fav' + $(el).attr('id')] == 'likes') { $(this).attr("disabled", "disabled"); } }); }; //console.log('also working'); var syncLikes = function () { var contentId = $('#details').find('.likeButton').attr('class').split(' '); if ($('#' + contentId[1]).is(":disabled")) { $('#details').find('.' + contentId[1]).attr('disabled', 'disabled'); } //console.log('im working'); }; var applyLike = function () { $(document).on("click", ".likeButton", function (e) { e.preventDefault() var contentId = $(this).attr("class").split(' '); //alert(contentId) $.ajax({ url: '@(Url.AbsoluteAction("LikeGalleryItem", ViewContext.RouteData.Values["controller"].ToString(), new { }))', contentType: 'application/json', data: { "contentId": contentId[1] }, success: function (msg) { //$('#captchaImage').html(msg); $('.' + contentId[1]).attr("disabled", "disabled"); //$(this).attr("disabled", "disabled"); localStorage.setItem('fav' + (contentId[1]) , 'likes'); } }); }); }; checkLikes(); applyLike();
Used this on a project for Amazon awhile back. Used this script in conjunction with the Magnific Popup plugin in order to let users "like" images in the gallery.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.