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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:14:53+00:00 2026-05-11T14:14:53+00:00

I’m trying to create a jQuery plugin, inside I need to do an AJAX

  • 0

I’m trying to create a jQuery plugin, inside I need to do an AJAX call to load an xml.

jQuery.fn.imagetags = function(options) {    s = jQuery.extend({      height:null,      width:null,      url:false,      callback:null,      title:null,   }, options);    return this.each(function(){     obj = $(this);      //Initialising the placeholder       $holder = $('<div />')     .width(s.width).height(s.height)     .addClass('jimageholder')     .css({         position: 'relative',     });     obj.wrap($holder);      $.ajax({       type: 'GET',       url: s.url,       dataType: 'xml',       success:function(data){ initGrids(obj,data,s.callback,s.title); } ,       error: function(data) { alert('Error loading Grid data.'); },     });      function initGrids(obj, data,callback,gridtitle){     if (!data) {       alert('Error loading Grid data');     }      $('gridlist gridset',data).each(function(){       var gridsetname = $(this).children('setname').text();       var gridsetcolor = '';       if ($(this).children('color').text() != '') {         gridsetcolor = $(this).children('color').text();       }       $(this).children('grid').each(function(){              var gridcolor = gridsetcolor;         //This colour will override colour set for the grid set         if ($(this).children('color').text() != '') {           gridcolor = $(this).children('color').text();         }          //addGrid(gridsetname,id,x,y,height,width)         addGrid(             obj,             gridsetname,             $(this).children('id').text(),             $(this).children('x').text(),             $(this).children('y').text(),             $(this).children('height').text(),             $(this).children('width').text(),             gridcolor,             gridtitle         );        });     });      }      function addGrid(obj,gridsetname,id,x,y,height,width,color,gridtitle){       //To compensate for the 2px border       height-=4;       width-=4;        $grid = $('<div />')         .addClass(gridsetname)         .attr('id',id)         .addClass('gridtag')         .imagetagsResetHighlight()         .css({         'bottom':y+'px',         'left':x+'px',         'height':height+'px',         'width':width+'px',          });        if(gridtitle != null){          $grid.attr('title',gridtitle);        }        if(color != ''){         $grid.css({         'border-color':color,         });       }       obj.after($grid);     }   }); } 

The above plugin I bind with 2 DOM objects and loads two seperate XML files but the callback function is run only on the last DOM object using both loaded XML files.

How can I fix this, so that the callback is applied on the corresponding DOMs. Is the above ajax call is correct?

Sample usage:

<script type='text/javascript'>         $(function(){             $('.romeo img').imagetags({               height:500,               width:497,               url: 'sample-data.xml',               title: 'Testing...',               callback:function(id){                 console.log(id);               },             });         });        </script>        <div class='padding-10 min-item background-color-black'>          <div class='romeo'><img src='images/samplecontent/test_500x497.gif' alt='Image'> </div>        </div>  <script type='text/javascript'>         $(function(){             $('.romeo2 img').imagetags({               height:500,               width:497,               url: 'sample-data2.xml',               title: 'Testing...',               callback:function(id){                 console.log(id);               },             });         });        </script>        <div class='padding-10 min-item background-color-black'>          <div class='romeo2'><img src='images/samplecontent/test2_500x497.gif' alt='Image'> </div>        </div> 

Here is the sample XML data:

<?xml version='1.0' encoding='utf-8'?> <gridlist>     <gridset>       <setname>gridset4</setname>       <color>#00FF00</color>       <grid>         <color>#FF77FF</color>         <id>grid2-324</id>         <x>300</x>         <y>300</y>         <height>60</height>         <width>60</width>      </grid>     </gridset>     <gridset>       <setname>gridset3</setname>       <color>#00FF00</color>       <grid>         <color>#FF77FF</color>         <id>grid2-212</id>         <x>300</x>         <y>300</y>         <height>100</height>         <width>100</width>      </grid>      <grid>         <color>#FF77FF</color>         <id>grid2-1212</id>         <x>200</x>         <y>10</y>         <height>200</height>         <width>10</width>      </grid>    </gridset> </gridlist> 
  • 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. 2026-05-11T14:14:54+00:00Added an answer on May 11, 2026 at 2:14 pm

    You might be experiencing issues because your calling the ajax load on same url, thus the second call cancels the first call.

    If you reading in the same url for each div, why don’t you call the ajax once then loop the elements when it returns.

    jQuery.fn.imagetags = function(options) {    s = jQuery.extend({      height:null,      width:null,      url:false,      callback:null,      title:null,   }, options);    var elems = $(this);    $.ajax({       type: 'GET',       url: s.url,       dataType: 'xml',       success:function(data){          elems.each(function() {                obj = $(this);            //Initialising the placeholder             $holder = $('&lt;div /&gt;')           .width(s.width).height(s.height)           .addClass('jimageholder')           .css({               position: 'relative',           });           obj.wrap($holder);            initGrids(obj,data,s.callback,s.title);          });       },       error: function(data) { alert('Error loading Grid data.'); }     });    return $(this); } 

    Basically, what your doing here is calling the ajax function then instantly return the set. then, when the ajax callback gets fired, your looping the elements and appending the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer depending on how you install the counter, (assuming transacted installation… May 11, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer You're looking for ReadOnlyCollection, which has been around since .NET2.… May 11, 2026 at 9:15 pm
  • Editorial Team
    Editorial Team added an answer If you mean std::map, it stores pairs of values. In… May 11, 2026 at 9:15 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.