Note that there are some explanatory texts on larger screens.

plurals
  1. POSet a variable to the data element
    text
    copied!<p>Setting up a Flickr script... In my ajax success function, how would I put a var in with the actual <strong>this</strong> element? I know <strong>this</strong> and <strong>$(this)</strong> represent different things. Hard to write out, so I'll use my code examples to explain better.</p> <p>I have my function set with the defined variables: <code>function flickr_feed(flickrID, flickr_count, photo_size) {</code>, which the variable in question is the <code>photo_size</code></p> <p>Using a condition to set the <code>getSize</code> variable from the readable format in <code>photo_size</code>:</p> <p><code>if (photo_size == 'square') var getSize = 'url_sq';</code></p> <p>I then dump that <code>getSize</code> into the ajax url:</p> <pre><code>url: 'http://api.flickr.com/services/rest/?&amp;method=flickr.people.getPublicPhotos&amp;api_key=' + flickrAPI + '&amp;user_id=' + flickrID + '&amp;per_page=' + flickr_count + '&amp;page=0&amp;extras=' + getSize + '&amp;format=json&amp;jsoncallback=?', </code></pre> <p>The problem I'm having in the success function is setting that <code>getSize</code> var to the element.</p> <pre><code>success: function(data) { $.each(data.photos.photo, function() { var photoID = 'http://www.flickr.com/photos/' + flickrID + '/' + this.id; var photoSrc = this.getSize; console.log(photoSrc); }); } </code></pre> <p>In this result, <code>photoSrc</code> returns undefined. But if I manually set <code>photoSrc</code> to <code>this.url_sq</code>, it's fine. Just can't figure out how to get the variable in there to work the same way.</p> <p>Apologies for the horrible explanation. If it makes sense at all, any help would be greatly appreciated.</p> <p><strong>EDIT:</strong> My experience with $.ajax is limited. But from what I gather, <strong>this</strong> refers to the returned data. So in that sense, the <code>photoSrc</code> returns undefined as it is thinking <code>getSize</code> is a result within the data, which it isn't. So how would that be written so that it would use the actual <code>getSize</code> value?</p> <p><strong>EDIT:</strong> Here's a pastebin of the script: <a href="http://pastebin.com/KGCUgAet" rel="nofollow">http://pastebin.com/KGCUgAet</a></p>
 

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