1

I can get this to work with a click on a PC, but not with a touch on a mobile device.

$('.img-responsive').on('click', function(){
    var src = $(this).attr('src');
    $('<div>').css({
        background: 'RGBA(0,0,0,.5) url('+src+') no-repeat center',
        backgroundSize: 'contain',
        width:'100%', height:'100%',
        position:'fixed',
        zIndex:'10000',
        top:'0', left:'0',
        cursor: 'zoom-out'
    }).on('click', function(){
        $(this).remove();
    }).appendTo('body');
});

Does anyone have any tips?

thanks!

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • try touchend and focusout events. ref : https://stackoverflow.com/questions/34065420/jquery-click-trigger-with-touch-devices – Nilanka Manoj Mar 15 '20 at 01:12

2 Answers2

0

Use the HTMLEvents object with document.createEvent() to simulate a click on a link.

    var link = document.getElementById( 'link_to_click' ),
    event = document.createEvent( 'HTMLEvents' );

    event.initEvent( 'click', true, true );
    link.dispatchEvent( event );
Omolewa Stephen
  • 444
  • 3
  • 19
0

try to delegatthe click event from the button’s parent or parent’s parent

$("body").delegate(".img-responsive", "click", function(){
//your action code
});

hope this work !, and may this link help you also https://www.codewall.co.uk/jquery-on-click-function-not-working-after-appending-html/

Nourhan Ahmed
  • 179
  • 1
  • 10