jQuery > Events

Error event in jQuery

How to attach an error event to a html element in jQuery?


To attach an error event to an html element, error() method can be used that executes if an error occured while loading that element

    <img src="idonotknow.jpg" id="imgError" />

    <script>
        $("#imgError").error(function () {
            alert("missing image");
        });
    </script>

In the above code, we have a img tag where we have specified src=”idonotknow.jpg” image, if this image doesn't exists on the server, error event will raise and alert "missing image" will be shown to the user. If this image exists on the server, no message is shown and image is loaded on the web page.

 Views: 6094 | Post Order: 66



Write for us






Hosting Recommendations