Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to store data retrieved through AJAX for later use
    primarykey
    data
    text
    <p>I have an application that opens a div with several thumbnails of images. When clicking on an image a new div shall open with the image in full size, and the div shall have the same width and height as the image.</p> <p>From the php file I retrive objects with several parameters, eg. thumbWidth/thumbHeight and width/height. What I need to do is to store the width and height for every image, so that I can open a div with the correct size. What is the best way to do it? I guess I could store width and height in a multi dimensional array, but I guess there is a better way?</p> <p>As you can see in the code below I tried to store eg. this.width in the variable 'imgWidth' and apply it to the event, but every image get the last retrieved width and height so that doesn't work.</p> <pre><code>$.getJSON('...getJSONThumbs.php', function(json) { $.each(json, function() { if (this.thumbWidth &gt; maxWidth){ maxWidth = this.thumbWidth; } if (this.thumbHeight &gt; maxHeight){ maxHeight = this.thumbHeight; } var box = $('&lt;div/&gt;', { 'class': 'imgDiv', 'width': maxWidth, 'height': maxHeight, }).appendTo('.imageArea:last'); var a = $('&lt;a/&gt;', { 'href': '#', }).appendTo(box) var img = $('&lt;img/&gt;', { 'src': 'pics/' + this.fileName, 'width': this.thumbWidth, 'height': this.thumbHeight, }).appendTo(a); imgWidth = this.width; imgHeight = this.height; box.click(function(event) { event.preventDefault(); console(imgWidth + " " + imgHeight); // always the last images width and height $('#desktop').css("background-image", "url(" + img.attr('src') + ")"); }); jQuery(loaderImage).hide(); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload