Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:05:51+00:00 2026-06-03T09:05:51+00:00

I have a bug in my code somewhere but can’t see what I have

  • 0

I have a bug in my code somewhere but can’t see what I have done wrong.

What I have is a hidden input of facebook user id’s which is populated via a jQuery UI autocomplete:

<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(',');
                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' });
};

When I click to send the vouchers, the error is from the alert dialogue in my else statement above:

Posting error occured

I added in another alert to see why this was happening after the opening of the loop:

$.each(referee, function() {
    referee = $.trim(referee);
    alert(referee); // new alert for debugging
    ....

What this alert outputs surprised me as the value of referee is 12345, 567890 (same as the hidden input but with trailing space and last comma removed).

Therefore, jQuery doesn’t seem to be splitting it correctly but it runs the loop the correct number of times as the alert box pops up twice telling me the referee and then the error. (both alert boxes show twice just to clarify). Incase this was a coincidence I tried adding more id’s to the hidden input and can confirm that the alert boxes are showing the same amount of times as the number of id’s.

So I was wondering what I had done wrong since the loop runs the correct amount of times but the comma separated id’s don’t seem to be split at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:05:53+00:00Added an answer on June 3, 2026 at 9:05 am

    There are a couple of problems with your code. As mentioned by others, you are looping an array using a non-existing .each() method. You can use a standard for() loop instead. Also it’s much more efficient stripping white space and excess commas using regular expressions.

    Here’s a simplified example, also available at http://jsfiddle.net/hJQ6W/

    <input id="fbid" type="hidden" value="12345, 567890, ">
    <script>
      var referees = $("#fbid").val();
      // trim string and remove all commas from the end of it
      referees = referees.replace(/\s/g,'').replace(/,*$/,'');
      var referee = referees.split(',');
      for(i=0; i<referee.length; i++) {
        alert('"'+referee[i]+'"');
      }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange bug, which I believe is caused by some code
Where do I have a bug? A mistake? Why can't I connect? My code,
I have an odd bug where my code returns a file not found exception
I don't why, but after 6h of searching the bug, I can't identify it.
I have a bug somewhere that is causing my app to just vanish without
I have code working on all desktop brosers, but in mobile it has a
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
I have bug checked this code considerably now and made sure I am outputting
i have bug that i cannot find, i have Class Point with method who
I have a bug I'm struggling to track down. I believe what's happening is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.