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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:51:12+00:00 2026-06-14T05:51:12+00:00

I made a quick function to check every link on the page using AJAX

  • 0

I made a quick function to check every link on the page using AJAX to see if they still work. This seems to be working, but it’s adding the success and error class to every one. How can I get the error callback function to only throw if the AJAX response is 404?

$('li').each(function(){
    $(this).children('a').each(function(){
        $.ajax({
            url:$(this).attr('src'),
            success:$(this).addClass('success'),
            error:$(this).addClass('error')
        })
    })
});
  • 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-14T05:51:14+00:00Added an answer on June 14, 2026 at 5:51 am

    the success and error parameters expect functions.

    You’d need to wrap the code in an anonymous function:

    //there's no need to complicate things, use one call to each()
    $('li > a').each(function () {
        var $this;
        $this = $(this); //retain a reference to the current link
        $.ajax({
            url:$(this).attr('href'), //be sure to check the right attribute
            success: function () { //pass an anonymous callback function
                $this.addClass('success');
            },
            error: function (jqXHR, status, er) {
                //only set the error on 404
                if (jqXHR.status === 404) { 
                    $this.addClass('error');
                }
                //you could perform additional checking with different classes
                //for other 400 and 500 level HTTP status codes.
            }
        });
    });
    

    Otherwise, you’re just setting success to the return value of $(this).addClass('success');, which is just a jQuery collection.

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

Sidebar

Related Questions

I have made a quick ajax driven page for pasting some text and running
I am using the following code to capture all the AJAX calls being made
I have a get/post/JSON function on an aspx page. This page adds data entered
I'm having trouble getting my callback function to work in post: I've got this
Consider this function I wrote real quick to find the last occurrence of a
I have a quick question about jQuery's AJAX function. The way I do my
I have made a quick fiddle: http://jsfiddle.net/tLLB4/ Ok, so the premise is, i'm hovering
Just a quick questions.. I have made an outlook add-in for 2007 version and
Made this nice little loop for hiding and showing div's, works as a charm
I made a Web service in which I have a function to count some

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.