eozzy
eozzy

Reputation: 68790

.click() inside a loop

I have this in my wordpress post loop:

function newWindow(uri,width,height) {
    if(!window.open(uri,uri,'scrollbars=1,toolbar=0,resizable=1,status=0,width='+width+',height='+height)) {
        document.location.href=uri;
    }
}
$('.facebook_button').click(function() {
    newWindow('http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>',720,420);
    return false; 
});

so when I click the button, it opens several windows (10 for 10 posts on the index page). Is there a way to open only for post the button was in?

Thanks!

Upvotes: 0

Views: 82

Answers (1)

Mike Brant
Mike Brant

Reputation: 71422

You should not have the javascript function definition inside the loop (it only needs to be output once).

I would suggest adding a unique id (maybe a hash of the_permalink() value) to each item in the loop, so you give yourself a good handle for the selector. So you would output in the loop something like

$('#<?php echo md5(get_permalink()); ?>').click(function() {
    newWindow('http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>',720,420);
    return false; 
});

Of course you have to add id="<?php echo md5(get_permalink()); ?>" also to the buttons you output.

Upvotes: 3

Related Questions