0

I want an event to be triggered after the creation of the corresponding HTML object (href in my case).

I've written this code:

$(document).ready(function() {
    $('body').on('click', '#stats-link', function(e) {
        console.log('TRIGGERED'); // nothing is logged
        e.preventDefault();
        $.post('stats.php', {'email': $('#email').val()}, function() {
            window.location = $(this).attr('href');
        });
    });

    $('#submit_button').click(function(e) {
        e.preventDefault();
        ...
        $('#info').html('<p class="desc"><a href="stats.php" id="stats-link">bla bla bla</a></p>');
        ...
    });
});

So, I make a href object identified by stats-link in the #submit_button function, then I want him to be triggered in the corresponding function (i.e., $('body').on(...), but it doesn't happen. What am I doing wrong?

2
  • 1
    If #submit_button is a submit button, you need to cancel that too: $('#submit_button').click(function(e) { e.preventDefault(); - laso you now ajax posts to stats.php then change the location to the same file. That does not seem right Commented Oct 22, 2014 at 9:06
  • Added. Nothing changes... Commented Oct 22, 2014 at 9:16

3 Answers 3

2

Did you missed this line:

$('#stats-link').trigger('click');

This will trigger the click event. The code you shared only binds the event. You need to trigger it.

JS:

$(document).ready(function () {
    $('body').on('click', '#stats-link', function (e) {
        e.preventDefault();
        alert('clicked')
        $.post('stats.php', {
            'email': $('#email').val()
        }, function () {
            window.location = $(this).attr('href');
        });
    });

    $('#submit_button').click(function (e) {
        e.preventDefault();
        $('#info').html('<p class="desc"><a href="stats.php" id="stats-link">bla bla bla</a></p>');
        $('#stats-link').trigger('click'); //This is where you trigger the click.
    });
});

Working Demo: http://jsfiddle.net/lotusgodkk/artaq4xj/

Sign up to request clarification or add additional context in comments.

8 Comments

Where do I need to put that?
You need to put it after you append that href object or anchor link.After this line: $('#info').html('<p class="desc"><a href="stats.php" id="stats-link">bla bla bla</a></p>');
@f_ficarola Check the demo, let me know if this is what you want.
So where is the problem? Remove the trigger line and it should work on your click too.
Yeah. Can you share your website link where you have this code?
|
1

What is e? Are you sure the click event isn't actually working, but just giving you a script error and bailing out before it does anything? Your callback function does not define the passed in event parameter e.

$('body').on('click', '#stats-link', function() { // <-- No e parameter supplied.
        e.preventDefault();  // <---- What is e?
        ...
}

1 Comment

Yes, thank you for your correction... I've forgotten the e in the function, but that wasn't the problem, the situation is still the same. I've tried to put console.log("AAAAAAAAAAAAAA");, but the console doesn't print anything and I don't have any error.
1

Your preventDefault() parameter is lacked :

$('body').on('click', '#stats-link', function(event) {
    event.preventDefault();
    $.post('stats.php', {'email': $('#email').val()}, function() {
        window.location = $(this).attr('href');
    });
});

1 Comment

Yes, I've already updated my post after the Lochemage's answer. I forgot the write e, but the situation does not change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.