Reputation: 708
Ref: Loading gravatar using jquery
I am using the above method to generate the gravatar on a page however I have multiple emails on the page and the code is only generating one MD5 and therefore only one gravatar my JS is:
var email = $('#email').val();
var digestString = Crypto.MD5(email);
$('#md5').val(digestString);
var gravatar = $('<img>').attr({src: 'http://www.gravatar.com/avatar/' + digestString});
$('.gravatar').append(gravatar);
I am using Crypto MD5 http://code.google.com/p/crypto-js/ and that is loaded in the header the emails are held in a hidden input:
<input type="hidden" id="email" value="<?php echo $message['contact_email'];?>">
These are printing out ok for each user.
Any help would be great
EDIT *****
HTML OUTPUT Working:
<div class="message-author">
<span class="gravatar" style="height: 24px;">
<img src="http://www.gravatar.com/avatar/82c9a9c254f35a912d8fefc248c30fd8"></span>
<input type="hidden" id="md5" value="82c9a9c254f35a912d8fefc248c30fd8">
<input type="hidden" class="email" value="[email protected]">
</div>
HTML OUTPUT not Working:
<div class="message-author">
<span class="gravatar" style="height: 24px;">
<img src="http://www.gravatar.com/avatar/82c9a9c254f35a912d8fefc248c30fd8"></span>
<input type="hidden" id="md5" value="">
<input type="hidden" class="email" value="[email protected]">
</div>
PHP
<?php foreach ($messages['page'] as $message): ?>
<li class="message">
<div class="message-body"><?php echo $message['message']; ?></div>
<div class="message-author">
<span class="gravatar" style="height: 24px;"></span>
<input type="hidden" class="md5" value="">
<input type="hidden" class="email" value="<?php echo $message['contact_email'];?>">
<?php echo $message['contact_person'];?> -
<?php echo date("m M - g:i a", $message['created']); ?>
</div>
</li>
<?php endforeach; ?>
Upvotes: 0
Views: 258
Reputation: 75317
Try the following:
$('input.email').each(function () {
var self = $(this);
var md5 = Crypto.MD5(this.value);
self.siblings('.md5').val(md5);
self.siblings('.gravatar').append($('<img />').prop('src', 'http://www.gravatar.com/avatar/' + md5));
});
You need to introduce a loop (via each()
) to consult each email
element individually; otherwise, as you realised, only the first is looked at (as getters such as val()
will only return the value of the first matched element.
However, as I hinted at in my comment, this should really be a job for PHP; it's also a potentially privacy issue sending users emails in your HTML.
Upvotes: 1