Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
jQuery ajaxError() Method
Next article icon

jQuery error() Method

Last Updated : 11 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

jQuery error() method in jQuery is used when an element encounters an error that is the element is not loaded correctly. The error() method attaches a function to run when an error event occurs or it triggers the error event.

Syntax:

Addition of function in error event:

$(selector).error(function)

Triggering of error event for some selected elements:

$(selector).error()

Parameters:

  • function: This parameter is optional and it specifies the function to be run when the error event occurs.

Example 1:

html

<!DOCTYPE html>
<html>
 
<head>
    <title>error() method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
 
    <script>
        $(document).ready(function () {
            $("img").error(function () {
                $("img").replaceWith(
                    "<h3>Error occurs</h3>");
            });
        });
    </script>
</head>
 
<body>
    <center>
 
        <h1>Welcome to GeeksforGeeks!.</h1>
        <div style="background-color:yellow;">
            <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190220214542/a13.png">
        </div>
        <div style="background-color:green">
 
            <h3> The image will be replaced with specified
                text if the image above encounters an error.
            </h3>
 
        </div>
    </center>
 
</body>
 
</html>
                      
                       

Output:

Example 2:

html

<!DOCTYPE html>
<html>
 
<head>
    <title>error() method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script>
        $(document).ready(function () {
            $("img").error(function () {
                $("img").replaceWith(
                    "<h3>Error </h3>");
            });
        });
    </script>
</head>
 
<body>
    <center>
        <div style="background-color:green">
            <h1>Welcome to GeeksforGeeks!.
            </h1>
        </div>
        <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190220220257/a22.png" 
             width="284"
             height="113">
 
        <h3>Image is replaced with a specified
            text, if error occur in loading.
        </h3>
 
    </center>
 
</body>
 
</html>
                      
                       

Output:

Note: This method was removed in jQuery version 3.0.



Next Article
jQuery ajaxError() Method

A

adeshsingh1
Improve
Article Tags :
  • JQuery
  • Web Technologies
  • jQuery-Methods

Similar Reads

  • jQuery ajaxError() Method
    The ajaxError() method in jQuery is used to specify a function to be run when an AJAX request fails. Syntax: $(document).ajaxError( function(event, xhr, options, exc) )Parameters: This method accepts single parameter function which is mandatory. This function accepts four parameters which are listed
    2 min read
  • JQuery .Deferred() method
    This JQuery.Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function. Syntax: jQuery.Deferred([beforeStart]
    2 min read
  • JQuery | isArray() method
    This isArray() Method in jQuery is used to determines whether the argument is an array. Syntax: jQuery.isArray( object ) Parameters: The isArray() method accepts only one parameter that is mentioned above and described below: object : This parameter is the object to test whether or not it is an arra
    2 min read
  • jQuery inArray() method
    This inArray() Method in jQuery is used to search for a specific value in an array and return its index (or -1 if not found). SyntaxjQuery.inArray(val, arr [, Index])Parameters The inArray() method accepts three parameters that are described below: val: The value to search in an array.arr: Any array
    2 min read
  • jQuery deferred.done() Method
    This deferred.done() method in jQuery is used to add handlers which are to be called when the deferred object is resolved. Syntax:  deferred.done(Callbacks [, Callbacks])Parameters: Callbacks: This parameter specifies a function, or array of functions, which are called when the Deferred is resolved.
    1 min read
  • jQuery getScript() Method
    The getScript() method in jQuery is used to run a JavaScript using AJAX HTTP GET request. Syntax: $(selector).getScript(url, success(response, status))Parameters: It contains two parameters as mentioned above and described below: url: It is a required parameter. It holds the url to whom the request
    2 min read
  • JQuery | isNumeric() method
    This isNumeric() Method in jQuery is used to determines whether its argument represents a JavaScript number. Syntax: jQuery.isNumeric( value ) Parameters: The isNumeric() method accepts only one parameter that is mentioned above and described below: value: This parameter is the value to be tested. R
    2 min read
  • jQuery ajax() Method
    The jQuery ajax() method is used to perform asynchronous HTTP requests, allowing you to load data from a server without reloading the webpage. It provides a flexible way to interact with remote servers using GET, POST, or other HTTP methods, supporting various data formats. Syntax:$.ajax({name:value
    4 min read
  • jQuery deferred.fail() Method
    The deferred.fail() method in jQuery is used to add handlers which are to be called when the Deferred object is rejected. This method accepts one or more than one arguments, which can be either a function or an array of functions. Callbacks are executed in the same order they were added. When the De
    2 min read
  • JQuery deferred.then() method
    This deferred.then() method in JQuery is used to add handlers which are to be called when the Deferred object is resolved, rejected, or in progress. Syntax: deferred.then(doneCallbacks[, failCallbacks][, progressCallbacks])Parameters: doneCallbacks: This is a function, or an array of functions, whic
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences