2

I want to pass Jquery object to function but its not working for IMG tag.

I made below example.When I click on the text it works , But when I click on image its not working.

$(document).on('click','.playvid',function (event) { event.stopPropagation(); popup($(event.target)); }); function popup(data) { data.html("success"); }
.playvid { cursor:pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div data-id="3ZdHRvPNyCI" class=" playvid" ><img src="https://cdn4.iconfinder.com/data/icons/iconset-addictive-flavour/png/button_green_play.png">WATCH DEMO</div>

3 Answers 3

5

The issue is because, depending on where you click, event.target can be the img element, which cannot contain html. Instead, pass $(this) to your function as that will contain the .playvid element:

$(document).on('click', '.playvid', function(event) { event.stopPropagation(); popup($(this)); }); function popup(data) { data.html("success"); }
.playvid { cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div data-id="3ZdHRvPNyCI" class="playvid"> <img src="https://cdn4.iconfinder.com/data/icons/iconset-addictive-flavour/png/button_green_play.png"> WATCH DEMO </div>

Sign up to request clarification or add additional context in comments.

Comments

2

You can simply pass the object by class -

popup($('.playvid')); 

For multiple .playvid class -

popup($(this)); 

1 Comment

I have multiple playvid class
0
$(document).on('click','.playvid',function (event) { event.stopPropagation(); popup($(event.currentTarget)); }); 

where currentTarget is all the time the last warping object of all objects having '.playvid' as rule.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.