I have an img tag under anchor tag, i dont want click on img tag, i tried disabled property, it seems there is no such property for img tag.Please let me know how to disable click on img.
code:
<a href="google"><img src="sample.png"></img></a>
Thankyou
I have an img tag under anchor tag, i dont want click on img tag, i tried disabled property, it seems there is no such property for img tag.Please let me know how to disable click on img.
code:
<a href="google."><img src="sample.png"></img></a>
Thankyou
Share Improve this question edited Feb 17, 2014 at 4:47 Java Learner asked Feb 14, 2014 at 6:35 Java LearnerJava Learner 3213 gold badges10 silver badges26 bronze badges 2-
The
click
event is on the<a>
element, why not just remove the hyperlink? – Jason Sperske Commented Feb 14, 2014 at 6:37 - @RajasekharP its working check the fiddle demo jsfiddle/gKb6B – Shaik Mahaboob Basha Commented Feb 14, 2014 at 7:14
3 Answers
Reset to default 3With CSS, you can use pointer-events property:
img {
pointer-events: none;
cursor: default;
}
You can check browser support for this property here.
Btw, <img></img>
is not valid HTML markup. You need to use <img />
Fiddle Demo
Check this question, is it the thing you want? Disable link using css
So that your code will be...
<a href="google." style="pointer-events: none; cursor: default;">
<img src="sample.png" />
</a>
By the way, in HTML the img tag has no end tag. http://www.w3schools./TAGS/tag_img.asp
To prevent the default behavior we can use event.preventDefault() method of the event. Older browsers (ie 7 and below) dont support this method, so we have to use onclick="return false;"
Note:
I added simple text with in the link, to demonstrate the image click is disabled but when we click the text, the anchor click gets activated.
function onimgclick(event) {
if(event.preventDefault)
event.preventDefault();
else {
return false;
}
}
And the html markup should be
<a href="google."><img onclick="return onimgclick(event)" src="sample.png"></img> HI</a>