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

  • Home
  • SEARCH
  • 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 6330997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:58:32+00:00 2026-05-24T17:58:32+00:00

Trying to get tag-it to work with an ajax call. Everything works so far.

  • 0

Trying to get tag-it to work with an ajax call.

Everything works so far. Except, I am unable to assign a tagSource via an ajax call.

In firebug, the ‘data’ is returning:

["Ruby","Ruby On Rails"]

But its not showing up as I type into the input box.

$('.tags ul').tagit({
  itemName: 'question',
  fieldName: 'tags',
  removeConfirmation: true,
  availableTags: ["c++", "java", "php", "javascript", "ruby", "python", "c"],
  allowSpaces: true,
  // tagSource: ['foo', 'bar']
  tagSource: function() {
    $.ajax({
      url:        "/autocomplete_tags.json",
      dataType:   "json",
      data:       { term: 'ruby' },
      success:    function(data) {
        console.log(data);
        return data;
      }
    });
  }
});

console.log(data) returns ["Ruby", "Ruby On Rails"].

Am I missing something here? Anyone else got this to work?

  • 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-05-24T17:58:33+00:00Added an answer on May 24, 2026 at 5:58 pm

    Seems this question hasn’t been answered for a long time, I bet you have already found a solution but for those who haven’t here is my answer:

    The indention got all messed up when i copied from my code 😉

    <input type="hidden" name="tags" id="mySingleField" value="Apple, Orange" disabled="true">
    Tags:<br>
    <ul id="mytags"></ul>
    
    <script type="text/javascript">
        jQuery(document).ready(function() {
        jQuery("#mytags").tagit({
            singleField: true,
            singleFieldNode: $('#mySingleField'),
            allowSpaces: true,
            minLength: 2,
            removeConfirmation: true,
            tagSource: function( request, response ) {
                //console.log("1");
                $.ajax({
                    url: "search.php", 
                    data: { term:request.term },
                    dataType: "json",
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                            return {
                                label: item.label+" ("+ item.id +")",
                                value: item.value
                            }
                        }));
                    }
                });
            }});
        });
    

    and the “search.php” you can find this in some autocomplete jQuery examples actually.

    <?php
    $q = strtolower($_GET["term"]);
    if (!$q) return;
    
    $items = array(
        "Great Bittern"=>"Botaurus stellaris",
        "Little Grebe"=>"Tachybaptus ruficollis",
        "Black-necked Grebe"=>"Podiceps nigricollis",
        "Little Bittern"=>"Ixobrychus minutus",
        "Black-crowned Night Heron"=>"Nycticorax nycticorax",
        "Heuglin's Gull"=>"Larus heuglini"
    );
    
    function array_to_json( $array ){
    
        if( !is_array( $array ) ){
            return false;
        }
    
        $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
        if( $associative ){
    
            $construct = array();
            foreach( $array as $key => $value ){
    
            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.
    
            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "\"".addslashes($key)."\"";
    
            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "\"".addslashes($value)."\"";
            }
    
            // Add to staging array:
            $construct[] = "$key: $value";
        }
    
        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";
    
    } else { // If the array is a vector (not associative):
    
        $construct = array();
        foreach( $array as $value ){
    
            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }
    
            // Add to staging array:
            $construct[] = $value;
        }
    
        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }
    
    return $result;
    }
    
    $result = array();
    foreach ($items as $key=>$value) {
        if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
    }
    if (count($result) > 11)
        break;
    }
    echo array_to_json($result);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Trying to get tag-it to work with an AJAX call How can
This is a question based on: Trying to get tag-it to work with an
I'm trying to get HTML5's audio tag to work in Chrome. The following code
In Haml, I've been trying to get the following link_to_remote call to work. It's
I'm trying to get a custom template tag to work but am having some
I'm trying to get this bit of code in my cfscript tag to work.
I have been trying to get this program to work but so far having
I'm trying to get the tag-it jquery plugin to work with a json string.
I'm trying to get meta tag information for each page on a particular site,
I had been trying to get the tag name and its value in java

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.