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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:52:04+00:00 2026-06-14T01:52:04+00:00

On window load I am trying to check if my image has already been

  • 0

On window load I am trying to check if my image has already been saved and if so change the HTML so that the link to save it is disabled. This code kind of works without the .each() with but only finds the first tag and changes all other similar tags based on that. Of course I want the do each tag separately. From looking at the post transactions in firebug, each ajax call processes properly and is sent the correct href value but the html updates don’t seem to take. The if statement isn’t failing because the alert message comes on when it should. Any idea why the $(this) works for retrieving the item value but doesn’t work when setting the html changes?

window.onload =  function() {
    $(".img a").each(function() {
        var item=$(this).attr( 'href' );
        var action="check";
        jqxhr = $.post("webservice.php", { action: action, color: item }, function(data) {
            var result=data.result;
            if (result=="saved") {
                $(this).html("<div style='line-height:4em '>Saved</div>");
                $(this).attr("href","saved");
                alert(result);
            }

        }, "json")
        .error(function() {         
            alert("error: unable to contact web service"); 
        });
    });
}
  • 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-14T01:52:05+00:00Added an answer on June 14, 2026 at 1:52 am

    The problem is that the this inside the callback does not refer to the element you think.

    You need to create a reference to that element before the ajax and use that.

    window.onload =  function() {
        $(".img a").each(function() {
            var self   = $(this),
                item   = this.href,
                action = "check";
            jqxhr = $.post("webservice.php", { action: action, color: item }, function(data) {
                var result=data.result;
                if (result=="saved") {
                    self.html("<div style='line-height:4em '>Saved</div>");
                    self.attr("href","saved");
                    alert(result);
                }
    
            }, "json")
            .error(function() {         
                alert("error: unable to contact web service"); 
            });
        });
    }
    

    Additionally it does seem like an overkill to run so many ajax call to check a similar thing.
    It might be a better idea to refactor your code to make a single ajax call, but pass it a list of files to check and then handle the response which would refer to all those images (one-by-one)

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

Sidebar

Related Questions

I'm trying to load an image from a given link var imgPath = $(imgLink).attr('href');
I have a deferred function that binds a mouseenter event: $(window).load(function(e) { var $container
I have been trying to use QCA ( Link ) on my Windows PC
I have been trying to call a different controller from a jsp page that
I am trying to load dlls into my program at runtime that match a
I have a situation that has been partially covered by other answers at SO,
I am currently trying to load some js files asynchronously, so that they are
I'm trying to load and host a WPF control in a .net 2.0 windows
I'm trying to write an abstraction to load dynamic libraries on Windows and Linux.
$(window).load(... waits for EVERYTHING on in the window to load. Which is great. But

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.