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 8971689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:01:14+00:00 2026-06-15T18:01:14+00:00

I have read the official API document but I don’t really get it. function

  • 0

I have read the official API document but I don’t really get it.

   function format(state) {
            if (!state.id) return state.text; // optgroup
            return "<img class='flag' src='images/flags/" + state.id.toLowerCase() + ".png'/>" + state.text;
        }
        $("#e4").select2({
            formatResult: format,
            formatSelection: format
        });

Here are the codes from select2 about how to format an element.

However, I don’t really understand the meaning of format(state) What does this state mean exactly?


I have a dropdown (data from an associative array, user_id=>username), now I want to format the dropdown result element so user can not only choose the username, but also they can see the user’s avatar beside the username.

here are some of my codes… but it doesn’t work.

<script>
$(document).ready(function(){
        $("#s2id2").select2({
            formatResult: function(){return "<img width='25px' height='25px' src='<?php echo $avatar;?>'/>" + "<?php echo $friend->username;?>";},
            formatSelection: function(){return "<img width='25px' height='25px' src='<?php echo $avatar;?>'/>" + "<?php echo $friend->username;?>";},
        });
});
</script>

Again, the structure of my data is “user_id=>username“…Any ideas?

  • 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-15T18:01:15+00:00Added an answer on June 15, 2026 at 6:01 pm

    Turns out i gave up using the javascript, instead, i use the following function:

    foreach($friends as $friend){
    $avatar = Profile::model()->findByPk($friend)->avatar;
    array_push_assoc($list,$friend2->sender,"<img width='20px;' height='20px;' style='float:left; margin-left:10px; margin-right:10px;' src='".$avatar2."'/>".$friend2->senderx->username);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read the document Authentication (official from Facebook). I still don't understand the
I have read the documentation of each function on jQuery official website , but
Sorry, although I read the official document on phoneGap, I still get confused about
I I have read this url first official and this another blog But they
I have read What's new in Xcode,but I can't find the official explanation for
I have read something about them via official doc and some posts, but I'm
ruby newbie alert! (hey that rhymes :)) I have read the official definition but
I have read the HOWTO on Unicode from the official docs and a full,
I have read few articles about table partioning but still I am bit confused
I have read some of the questions and answers here, but it none match

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.