0

I am trying to append the parent node with child node using jquery and object oriented javascript.

Can you please explain why the commented line in the buildexpenseTable function block does not work.

But the next line works fine.

var BUILD = BUILD || {};

BUILD.protoType = {
    expenseDiv : function(bearer){
        return "<P>hai</p>";
    }
}

BUILD.builder = {
    bearer : $("#bearer"),
    desc : $("#description"),
    amount : $("#amount"),
    output : $("#outputBlock"),
    buildExpenseTable : function (){
        //BUILD.builder.output.append($(BUILD.protoType.expenseDiv(bearer.value)));
        $("#outputBlock").append($(BUILD.protoType.expenseDiv(bearer.value)));
    }
}
2
  • what does "does not work" mean? which line does not work? also bearer.value may be falsy since bearer is a jQuery object and hence .value might not work here Commented Jul 20, 2014 at 9:30
  • The commented line in the code sample does not append the child node. bearer.value does not cause any issues It returns the value without any issue. Commented Jul 20, 2014 at 9:40

1 Answer 1

1

$ is a function. output is populated (and thus $('outputBlock') is executed) when BUILD.builder is created. If you use the second statement, $('outputBlock') is executed when buildExpenseTable is called.

Most probably, BUILD.builder is initialized before the page has been entirely loaded and the DOM has been built, so BUILD.builder.output will be empty.

Note that, further, the way you do it will cause $('#bearer'), $('#description'), etc. to be executed only once, namely at the time BUILD.builder is created; thus any subsequent changes to the DOM are ignored. That's probably not what you want?

You can solve the problem by doing this:

BUILD.builder = {
    bearer : function () { return $("#bearer") },
    desc : function () { return $("#description") },
    amount : function () { return $("#amount") },
    output : function () { return $("#outputBlock") },
    buildExpenseTable : function (){
        BUILD.builder.output ().append($(BUILD.protoType.expenseDiv(BUILD.builder.bearer ().value)));
    }
}

If you are absolutely sure that you really want a "once-and-forever" initialization, put the initialization of BUILD.builder into a $(document).ready (...) handler, yet I would rather not do that.

Second mistake: You probably won't find bearer, as you did not indicate the object where to look for it.

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

5 Comments

i have called the output in this line BUILD.builder.output.append($(BUILD.protoType.expenseDiv(bearer.value))); . but why it doesnt work.
No, you haven't called output, as output is not a function. You've populated output with the result of $('#outputBlock') once, and then you're simply reading the stored value. If $('#outputBlock') does not find the element at the time BUILD.builder is created, you won't ever find it with your method, as you do not execute $('#outputBlock') a second time.
Thanks for the explanation john. I am intializing it only one time using this statement.var BUILD = BUILD || {}; If i intialize Build every time, will my line of code BUILD.builder.output.append($(BUILD.protoType.expenseDiv(bearer.value))); executes with out any issues. Am I getting it correct?
I understand why my line wont work.but the second mistake does not valid isnt it? john. bearer.value will return the stored values without any issues. Because i could not find any issue in the browser.
Well, then you have another variable bearer somewhere, I guess? I am pretty sure that your bearer is not identical to BUILD.builder.bearer.

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.