0

I'm using this piece of code to wrap a link around a (dynamically) created div, which works good so far in firefox, but not on safari/chrome and ie.

I already know that I should use pointer events, but I'm not sure how to achieve this, since I still need the ".on" event, because of the dynamically created div.

// Create a link around the ID
$(".psv-hud").on('mousedown', '#psv-marker-job1', function() {
    $(this).wrap( "<a href='/psv-marker-job1/'></a>" );
});

Any ideas how I could solve this?

Johnny Kermode
  • 75
  • 2
  • 12
  • is this element was present at the time when you executed the jQuery code `.psv-hud` if it was not present the code won't work. – Kaushik Sep 11 '18 at 06:56
  • Is this code within `$(document).ready(function(){});`? – Rohit Sharma Sep 11 '18 at 06:58
  • @RohitSharma Yes it is, but this was not the problem. My code is working in firefox, but not on other browser, because mousedown is no longer supported. I need a solution to bypass mousdown with a pointer event, but I dont know how... – Johnny Kermode Sep 11 '18 at 07:06

2 Answers2

0

You need to specify an element that is already there when the DOM is created. In the parameters, you specify the elements you want to add the mousedown method. By simply assigning $('.enemy'), it will attach the method to those that are already present in the DOM.

$('body').on('mousedown', '.enemy', function(event) {
    //attack code
}

Jquery on mousedown not working on dynamically generated elements

Arne Kemps
  • 108
  • 10
  • Thanks for your afford, but this was not the problem. My code is working in firefox, but not on other browser, because mousedown is no longer supported. I need a solution to bypass mousdown with a pointer event, but I dont know how... – Johnny Kermode Sep 11 '18 at 07:04
  • Oh, maybe you could try using the native Web API? Just a thought. Examples: https://blog.garstasio.com/you-dont-need-jquery/events/#mouse-events – Arne Kemps Sep 12 '18 at 13:31
0

If this code don't work, .psv-hub isn't exist when mousedown event bind.

First, check change .psv-hub -> document.

$(".psv-hud").on('mousedown', '#psv-marker-job1', function() {
    $(this).wrap( "<a href='/psv-marker-job1/'></a>" );
});

Show me the example page.

seunggabi
  • 1,699
  • 12
  • 12
  • Thanks for your afford, but this was not the problem. My code is working in firefox, but not on other browser, because mousedown is no longer supported. I need a solution to bypass mousdown with a "pointer event", but I dont know how... – Johnny Kermode Sep 11 '18 at 07:05