Good, no matter how many laps I give an action. The case is that through ajax I call a php file that prints an html tag with echo. The problem comes when you want to handle that label (to which I have put a class) because, I presume, that is because it does not exist in the DOM.
How can I create the element in the DOM from php so I can access the element from javascript?
Here is the example that simplifies the project I am working on. A h2 in which clicking, the response of php appears. But when you click on the created paragraph, you do not recognize the class.
$contenido=$('.contenido');
$btn=$('.click');
$p=$('.parrafo');
$btn.on('click', function() {
$.ajax({
url: 'prueba.php',
success: function(response) {
$contenido.html(response);
}
})
});
$p.on('click', function() {
alert('Existo!');
});
<html>
<head>
<meta charset="UTF-8">
<title>Prueba</title>
</head>
<body>
<h1 class="titulo">Prueba</h1>
<p>Esto es una prueba</p>
<h2 class="click" style="cursor:pointer">Click aqui para enseñar contenido php</h2>
<p class="contenido"></p>
</body>
</html>
<?php
echo '<p class="parrafo">Hola mundo</p>';
?>
Also try with this php code using the DOMDocument class
<?php
$doc=new DOMDocument();
$doc->loadHTML('<p class="parrafo">Hola mundo</p>');
echo $doc->saveHTML();
?>
Also I have also tried without ajax, loading an html file from javascript with .load (), but I can not find the way.