JQuery on button click not working

I am using the jquery mobile click function, however it does not work.

Here is an example of a button that I have, and it is contained in the grid:

<div class="ui-block-c"><a class="request" data-role="button" data-id="\"'+json[i].num+'\" data-type="3" data-icon="plus" data-iconpos="right">Test</a></div> 

JQuery Function:

 $('.request').on('click', function() { alert("hi"); }); 

How to fix it?

+6
source share
2 answers

It looks like you are adding this element dynamically, so you need to use a delegated event listener:

 $(document).on('click', '.request', function() { alert("hi"); }); 

Also you have a problem with your escaped quotes that don't match. I do not think this is necessary:

 <div class="ui-block-c"><a class="request" data-role="button" data-id="'+json[i].num+'" data-type="3" data-icon="plus" data-iconpos="right">Test</a></div> 
+16
source
 $(.request).click(function(){ alert("hi") }); 
-4
source

Source: https://habr.com/ru/post/953352/