Because the expression dynamic: $('#foo')
is evaluated at the beginning of the script execution when the element with id foo
is not created so $('#foo')
will return an empty jQuery object.
The #foo
element is created only when the test1
method is called.
A possible solution is to store the dynamic element selector in a.dynamic
instead of trying to store the jQuery object like
dynamic: '#foo'
then
$(a.dynamic).html(m)
Demo: Fiddle
In the above case the selector #foo
is not evaluated till the test2
is called, by that time test1
has added the target element to the dom