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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:43:51+00:00 2026-06-01T05:43:51+00:00

I have an image tag enclosed in an anchor element like this: <a href=’$image’><img

  • 0

I have an image tag enclosed in an anchor element like this:

<a href='$image'><img alt='No Image' src='$image'></a>

I find that if the image is absent, I can still click on the link. I want to disable the link if the image is absent. What is the best way to do this?

Update:

I have tried mplungjan’s solution below but it didn’t work. I am willing to try jquery if javascript can’t do the job.

  • 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-01T05:43:54+00:00Added an answer on June 1, 2026 at 5:43 am

    This version works:

    <a href='$image'>
        <img alt='No Image' src='$image' onError="this.parentNode.onclick=function() {return(false);}">
    </a>​
    

    You can see it in action here: http://jsfiddle.net/jfriend00/2jb4G/

    Or, using a common function that you can use in multiple places:

    <script>
    function blockParentLink() {
        this.parentNode.onclick = function() {return(false);}
    }
    </script>
    
    <a href='$image'>
        <img alt='No Image' src='$image' onError="blockParentLink()">
    </a>​
    

    Personally, I think it might just make sense to hide it if it doesn’t display rather than block clicks:

    <a href='$image'>
        <img alt='No Image' src='$image' onError="this.parentNode.style.display = 'none';">
    </a>​
    

    You can see the hide version here: http://jsfiddle.net/jfriend00/KVUUM/

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

Sidebar

Related Questions

I have the following image tag on a page: <img src=http://photo.com/img/p806792255-6.jpg id=will6-1 alt=will6-1 title=will6-1
I have an image Tag like <img src=abc height=20 width=50 /> How can I
I have for example this image tag: <img src=http://... .jpg al=myImage hhh=aaa /> and
When I have an <a> tag set to a specific image background like this:
I have the following image tag in a JSP: <img id=validationGraph src=/myapp/MyServlet?mode=myMode usemap=#validationMap border=0
I have a user avatar on my website. Simple image tag: <img src=foo.jpg class=userphoto
When I have an image from 950x500 and output it like so: <img src=http://full/url/to/image.png
i have image collection @images = Image.find(:all) and displaying it like this <%@images.each do|image|%>
I have an image tag.. <img src=/folder1/folder2/image.jpg> I need to, using javascript / query
I have an image tag that is in a popup. This popup shows an

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.