martincarlin87
martincarlin87

Reputation: 11062

Struggling with JQuery .each() Function

Hopefully this will be a straightforward solution but I can't seem to solve it myself.

The background is I am trying to create a web app that puts all your facebook friends into a jQuery UI auto-complete and posts to their wall when you are finished selecting friends and click a link/button etc.

I had this working fine for one friend but the requirements have changed (as ever) to make this work for multiple friends.

So far I have everything working with multiple friends except when it comes to actually posting to the walls of the user's friends.

Code

I will try to keep this to the code relevant to the question which I believe is as follows:

I have a hidden input which I manage to populate with the ids:

<input id="fbid" type="hidden" value="12345, 567890, ">

I then have a jQuery function which runs when the link is clicked to post to the walls of the friends.

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId      : '12345678', // App ID
        channelUrl : '/channel.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        oauth      : true, // enable OAuth 2.0
        xfbml      : true  // parse XFBML
    });

    // Additional initialization code here

    FB.login(function(response)
    {
        if (response.authResponse)
        {                                               
            $("#send-voucher").click(function() {

                // Post message to friend's wall

                var opts = {
                    message : 'This is the message',
                    name : 'This is the name',
                    link : 'http://www.opticalexpress.co.uk',
                    description : 'This is the description',
                    picture : '/voucher_valid.png'
                };

                var referees = $("#fbid").val();

                // remove last comma and space from the end of the string
                referees = $.trim(referees);
                referees = referees.substring(0, referees.length - 1);

                var referee = referees.split(', '); // comma space
                referee.each(function() {

                    FB.api('/'+ referee +'/feed', 'post', opts, function(response)
                    {
                        if (!response || response.error)
                        {
                            alert('Posting error occured');
                        }
                        else
                        {
                            alert('Success - Post ID: ' + response.id);
                            $("#send-voucher").hide();
                            $("#voucher-sent").fadeIn('slow');
                        }
                    });
                });
            });
        }
        else
        {
            alert('Not logged in');
        }
    }, { scope : 'publish_stream' });
};

The error reported by firebug is referee.each is not a function

If you require any further information then please just let me know.

Upvotes: 0

Views: 176

Answers (3)

shadyyx
shadyyx

Reputation: 16065

You have to call it like this:

$.each(referee, function() { ... });

The jQuery.each() needs a collection as a first parameter then a callback function.

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337713

You can only use each() as you are on a jQuery object. Your referees variable is a array of strings, so you need to call it from the base $ jQuery object and pass it as a parameter, like this:

$.each(referee, function() {
    // ...
} 

Alternatively you can wrap your variable in a jQuery object, like this:

$(referee).each(function() {
    // ...
}

Upvotes: 1

MrCode
MrCode

Reputation: 64536

It needs the jQuery wrapper, change this:

referee.each(function() {

To:

$(referee).each(function() {

Upvotes: 2

Related Questions