3

I have the following HTML snippet contained in a listview. Each element in the listview begins with a thumbnail.

<li>
<a href="javascript:alert("don't want to see this alert);" >
<img src="/images/testimage_thumb.png" onclick="displayImage('/gallery/testimage.jpg');">
<h4>Test Image</h4>
<p>Description of test Image</p>
</a>
</li>

<li>
<a href="javascript:alert("don't want to see this alert either);" >
<img src="/images/testimage2_thumb.png" onclick="displayImage('/gallery/testimage2.jpg');">
<h4>Second Test Image</h4>
<p>Description of the Second Test Image</p>
</a>
</li>

Works like a champ (at least as far as displaying). I want to be able to have the users click on the thumbnail to display a larger image and NOT trigger the underlying anchor link (which in this case displays an alert message).

I'm sure the answer lies with preventDefault(), just not sure how to bind the click event to a function that will allow me access to the event object. There will be multiple thumbs in the listview and the list is generated dynamically in response to another event. I use the pagebeforeshow() event when then page is loaded to generate the list.

mlewis54
  • 2,372
  • 6
  • 36
  • 58
  • The code depends where you store path large img. But here's a hint `$(document).on('click', 'li a img', fucntion(e) { e.preventDefualt(); your code here });` – Omar Jun 12 '13 at 21:04

1 Answers1

5

Just add a click handler to your images.

$('img.ui-li-thumb').click(function(){
    alert('I am the image');
    return false;
});

This will bind a click handler to your images that have the ui-li-thumb class. Basically all the images that are thumbnails. Customize the selector further to apply it to a specific ul if you need to. This is a class added dynamically by jqm. return false will call

  1. preventDefault (which in your case doesn't do anything because there is no event on the img tag.)
  2. stopPropagation (which it what you want. This will stop the bubbling of the event - by bubbling I mean calling the handler of the a tag that you have your img in.)

The above code can be replaced by

$('img.ui-li-thumb').click(function(e){
    e.stopPropagation();
    alert('I am the image');
});

check here for a demo . Didn't have an actual image.

And going a little beyond what's been asked, check this What's the difference between event.stopPropagation and event.preventDefault?

UPDATE

$('img.ui-li-thumb').click(function(e){
    e.stopPropagation();
    alert(this.src);
});

UPDATE 2

My bad, the OP said that should consider dynamic elements.

$('div[data-role="content"]').on('click', 'a', function(){

    alert(1);
});

$('div[data-role="content"]').on('click', 'img.ui-li-thumb', function(e){
   //e.preventDefault();
    e.stopPropagation();
    alert(2);
});

This is the right one. After jquery 1.7 you can use on for this kind of binding (that will take care of the binding on dynamic elements).

I made a change though. Because of the different way on() works, I added e.preventDefault. Although the hanlder of the 'a' tag is not fired (so the alert(1) is not showing what so ever), it leaves the default behavior of a link tag to follow the link. So you add both of them, or you remove them, and add at the end

return false; 

check demo

Community
  • 1
  • 1
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • `preventDefault()` is essential here, if you add a link to the anchor, it will trigger. – Omar Jun 12 '13 at 21:06
  • The img handler is being called first, so the stoppro.. prevents it from bubbling. – Alkis Kalogeris Jun 12 '13 at 21:10
  • @alkis Is there something in the passed event object that let's me distingush between each individual image that so I can display the larger image? In the onclick code I am passing the image name in. – mlewis54 Jun 12 '13 at 21:24
  • I added an update. By 'this' you can access src, or whatever other attribute you want. Please don't forget to mark as answered – Alkis Kalogeris Jun 12 '13 at 21:29
  • 1
    @mlewis54 you're welcome. You can save img path as a custom attribute like `data-img=../gallery/img.jpg` and retrieve it `$(this).data('img');`. – Omar Jun 12 '13 at 21:36
  • Is there something I need to put in my img tag. I added the following after all list items: $('img.ui-li-thumb').click(function(e){ e.stopPropagation(); alert(this.src); }); The event never gets triggered, just the underlying anchor link. My image tag looks like: – mlewis54 Jun 12 '13 at 21:44
  • Sorry about that. Forgot that you wanted for dynamic elements. I'll change my post in a bit – Alkis Kalogeris Jun 12 '13 at 21:58
  • 1
    @mlewis54 bind events this way `$(document).on('click', '.selector', function()` – Omar Jun 12 '13 at 22:10
  • Ready to go. Added a demo and some clarifications. Omar check this odd behavior. Even if I remove the preventDefault, alert(1) is not shown, but it attempts to visit the link (you'll see a refresh on the page). So I added the preventDefault to nullify it completely. – Alkis Kalogeris Jun 12 '13 at 22:13
  • Well in my defense it's a different method. But yes you were almost right. The event doesn't fire, just the attempt to follow the link. It's pretty odd actually. http://jsfiddle.net/bujar/ayYpc/9/ check it out. The prevent is removed. – Alkis Kalogeris Jun 12 '13 at 22:29
  • Check this, I'm using iPad so i have limited access to test it thoroughly. I'll take my up vote back ;) http://jsfiddle.net/Palestinian/ayYpc/11/ – Omar Jun 12 '13 at 22:35
  • 1
    No, it still has that odd behavior and it doesn't work for the dynamic element. on() has to have a static element as a start (initial selector). I think this would make a good question – Alkis Kalogeris Jun 12 '13 at 22:41
  • Go ahead and investigate more about it. Ill check it tomorrow, iPad is useless. It's good for "clash of clans" hahaha – Omar Jun 12 '13 at 22:46