href does not execute jQuery and Function

Asked

Viewed 73 times

0

When you click on the Reply link the code does not execute, it does not show the jQuery Alert or Function Alert, tbm does not show an error in the console, 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');
}
  • Try changing the first line like this: $('#post-forum').last().append('<div id="responder"><a href="#" >Responder</a></div>');

  • Thanks, I just tried, and tbm didn’t work, it tries to take the value of href.

1 answer

1

I noticed you were pointing a .last() for a id in the first line of your code, which most certainly should be the reason why the code is not running.

id's are to be used only once in the DOM.
To identify more than one element with the same name, we should 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>

<div class="post-forum">1</div>
<div class="post-forum">2</div>
<div class="post-forum">3</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.