rr-
rr-

Reputation: 14811

Jquery .on doesn't seem to work with AJAX

Why following doesn't work:

$(function()
{
    $('#settings').on('click', '.link', function(e)
    {
        e.preventDefault();
        alert('okay');
    });
});

...when just prepending body to selector makes it work?

$(function()
{
    $('body #settings').on('click', '.link', function(e)
    {
        e.preventDefault();
        alert('okay');
    });
});

This code is executed by $.getScript(...) call just after #settings is inserted into DOM by $('body').append(...).
I want to use .on() since .links can be created dynamically and I don't want to rebind events all over the place.

Browsers affected: Firefox, Chrome, possibly more
JQuery version: 1.11.0

Upvotes: 3

Views: 47

Answers (1)

Sergio
Sergio

Reputation: 28837

ID's must be unique.

In this case it seems you might have more than one element with the same ID, thus the odd behaviour you are getting.

Upvotes: 1

Related Questions