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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:32:51+00:00 2026-05-23T00:32:51+00:00

I developed a simple plugin to sent request to a JSON text file, retrieve

  • 0

I developed a simple plugin to sent request to a JSON text file, retrieve a data containing image list and append the html in the element calling the plugin.

Now, the code is working fine while it is running on only one element, but when I am using it on more than one element, now the elements get loaded on the last element calling the plugin only.

Plugin Code:

$.fn.testPlugin = function(param) {
    var options = {
        url : "",
        pause : 2000,
        callback : 5000
    };
    this.each(function() {
        elementId = $(this).attr("id");
        options = $.extend(options,param);
        if(options.url=="") { console.log("URL is not specified"); }
        else {
            $.post(
                options.url,
                function(data) {
                    $("#"+elementId).html("<ul></ul");
                    $.each(data.dashboard, function(k,v) {
                       html = '<li>';
                       html += '<a href="'+v.TargetUrl+'" target="'+v.Target+'">';
                       html += '<img src="' + v.ImageUrl + '" alt="' + v.Alt +'" title="' + v.OverlayText +'" />';
                       html += '</a><p>'+v.OverlayText+'</p></li>';
                       $("ul",$("#"+elementId)).append(html);
                    });
                },
                "json"
            );
        }
    });

}

Plugin Initialization/Execution

$("#this").testPlugin({
    url: 'test.txt'
});
$("#that").testPlugin({
    url: 'text.txt'
}); 

HTML

<div id="this" style="border-style: solid;border-color: #09f;padding: 10px;">
This is a sample div.
</div>

<div id="that" style="border-style: solid;border-color: #09f;padding: 10px;">
Another div
</div>  

UPDATE

I also found out that the problem is happening due to AJAX request. If I create static list and then append it on the div, this time this works with any number of instantiation. A demo of static call is here. Now, help me do the same by retrieving the list from an AJAX request.

UPDATE 2

Thanks to Groovek, Here is a demo of the actual problem. You can notice, that the elements of both requests are append to the last element.

  • 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-23T00:32:51+00:00Added an answer on May 23, 2026 at 12:32 am

    You’re assigning to elementID without declaring it. This means it’ll be a global variable, which means it’ll always be equal to the last thing assigned to it (in this case, #that). If you just keep a local reference to the element, the code will work. Here’s a modified jsfiddle: http://jsfiddle.net/s9BDT/

    Code inside the each ‘loop’:

            var el = $(this);
            el.html("<ul></ul>");
            options = $.extend(options,param);
            if(options.url=="") { console.log("URL is not specified"); }
            else {
                $.post(
                    //options.url,
                    "/echo/json/",
                    {json:'{"dashboard":[{"TargetUrl":"toto.html", "Alt" : "sample 1", "ImageUrl":"toto.png", "OverlayText":"toto"},{"TargetUrl":"titi.html", "Alt" : "sample 2", "ImageUrl":"titi.png", "OverlayText":"titi" }]}'},
                    function(data) {
                        //alert(data.dashboard);
    
                        html = '';
                        $.each(data.dashboard, function(k,v) {
                           html += '<li>';
                           html += '<a href="'+v.TargetUrl+'" target="'+v.Target+'">';
                           html += '<img src="' + v.ImageUrl + '" alt="' + v.Alt +'" title="' + v.OverlayText +'" />';
                           html += '</a><p>'+v.OverlayText+'</p></li>';                           
                        });
                        //alert(html);
                        $("ul",el).append(html);
                    },
                    "json"
                );
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a simple mechanism for my mvc website to pull in html
I've developed an equation parser using a simple stack algorithm that will handle binary
I've just developed a simple page, using the standard jQuery tabs jQuery Tabs Link
I have developed a simple library in Ruby and need to use this in
I have a simple question: Would you rather use a plugin that is actively
A colleague developed a IE Plugin which I require to run for a piece
I've developed an osgi application (no rcp) using eclipse. It consists of several Plugin-Projects-Bundles
I am trying to run a simple SQLITE application on Windows Mobile developed with
I wish I can make an image gallery without using jQuery plugin. Which all
I developed a simple webpart using the Visual Webpart template in Visual studio 2010

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.