1

I need to add to DOM some html´s by jquery, and bind some events the generated elements, but i cant syncronize it, where the addEvents function starts, the DOM elements are not created, so the $(".login-log") element is not on DOM yet.

I found this:

Javascript Event Synchronization

Im working on it but dont works for me, that my code, i dont know if i miss something or what:

var Login = function ()
{
var commons = new Commons();

this.init = function()
{
    stepOne(stepTwo);

    commons.init();
}

function stepOne(callback) {
    var AsyncDone = function()
    {
        callback();
    }
    loadFiles(AsyncDone);
}

function loadFiles(callback)
{
    $(".header-container").load("views/header.html");
    $(".content-container").load("views/login.html");
    callback();
}

function stepTwo() {
    addEvents();
}

function addEvents() {
    alert("is here");
    $(".login-log").bind("click", function() { alert("fuck"); });
}
}

The syncronizathion makes the alert "is here" to appear before the DOM elements of header and login.html are loaded in DOM.

I know that have to be simple, but i dont find the solution.

Thanks in advice.

4
  • What doesn't work? You need to improve this question to ask a specific question, see the stackoverflow.com/faq Commented Feb 15, 2013 at 9:17
  • I need to add to DOM some html´s by jquery, and bind some events to that elements i generate, but i cant syncronize it, where the addEvents function starts, the DOM elements are not created, so the $(".login-log") element is not on DOM yet. Commented Feb 15, 2013 at 9:29
  • I think is clear here: "The syncronizathion makes the alert "is here" to appear before the DOM elements of header and login.html are loaded in DOM." Dont put a negative vote and try to understand the question before... Commented Feb 15, 2013 at 9:31
  • Your loadFiles function invokes the callback before the two load has happened. Btw, those stepOne and stepTwo are completely unnecessary, just call loadFiles(addEvents) Commented Feb 18, 2013 at 8:25

1 Answer 1

1

My final choose:

this.init = function()
{
    loadHeader(addHeaderEvents);
    loadTemplate(addTemplateEvents);
    loadFooter(addFooterEvents);

    commons.init();

}

function loadHeader(callback) {
    $(".header-container").load("views/header.html", function() {
        callback();
    });
}
function addHeaderEvents() {

}

function loadTemplate(callback) {
    $(".content-container").load("views/template_login.html", function() {
        callback();
    });
}
function addTemplateEvents() {
    alert("llega");
    $(".login-log").bind("click", function() { alert("done"); });
}

function loadFooter(callback) {
    $(".footer-container").load("views/footer.html", function() {
        callback();
    });
}
function addFooterEvents() {

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

Comments

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.