href does not execute jQuery and neither function

0

When I click on the Reply link, the code does not execute, it does not show the jQuery alert or the function, it does not show an error in the console, it does not even execute.

$('#post-forum').last().append('<div><a href="#" id="responder" >Responder</a></div>');

$('#responder').on('click', function (event) {
    // alert('teste');
    teste();
    event.preventDefault();
});

function teste() {
 alert('teste');
}
    
asked by anonymous 15.04.2017 / 04:52

1 answer

1

I noticed that you are pointing a .last() to a id in the first line of your code, which most certainly should be the reason the code is not executing.

  

id 's are only to be used only once in the DOM.   To identify more than one element with the same name, we must use classes .

$('.post-forum').last().append('<div><a href="#" id="responder" >Responder</a></div>');

$('#responder').on('click', function (event) {
    // alert('teste');
    teste();
    event.preventDefault();
});

function teste() {
    alert('teste');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><divclass="post-forum">1</div>
<div class="post-forum">2</div>
<div class="post-forum">3</div>
    
15.04.2017 / 08:48