Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Check the broken image link and replace it with alt text in jQuery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 292
    Comment on it
    Check if image link is broken in page and replace it with the alt text and open the blank page when user click on it

    $('img').each(function(  ) {
                $( this ).error(function(){
                            var img_path = $(this).attr('src');
                            var img_alt = $(this).attr('alt');
                            $(this).after('<a href='+img_path+' target=_blank>'+img_alt+'</a>');
                            $(this).hide();
    });
    

    In above code-

    $('img').each(function( ) { : This function will search all image link on the page

    $( this ).error(function(){ : this will check whether image exists or not.

    var img_alt = $(this).attr('alt'); : it will get the alt attribute of the image link.

    $(this).after(''+img_alt+''); this will replace the entire image with the alt text or alt attribute.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: