jQuery gets confused if used from a fragment loaded by jQuery itself
I have a problem with jQuery in a page which contains few divisions.
Let us consider the following HTML code:
- <div id="a"><a href="javascript:/" onclick="LoadB();return false;">Load B</a></div>
- <div id="b"></div>
- <div id="c">TEST</div>
where
- function LoadB( ) {
- $( "#b" ).load( 'b.html' ) ;
- }
and b.html contains
- <a href="javascript:/" onclick="LoadC();return false;">Load C</a>
where
- function LoadC( ) {
- $( "#c" ).load( 'c.html' ) ;
- }
and c.html contains
If I click on "Load B" it works but, as soon as the second link appears, if I click on "Load C", it does not. I get the following error:
Uncaught TypeError: Cannot read property 'body' of undefined
I suspect that in the second case there is some problem for jQuery to navigate the DOM.
Just consider that if I have check for division C after the first click, getElementById and jQuery give two different results. In fact
- console.log(document.getElementById('c'));
- console.log($( "#c" ));
result into
- <div id="c">TEST</div> (from getElementById)
- funzioni.js:68 (from jQuery)
- w.fn.init [div#c]
- 0: div#c
- length:1
- __proto__:Object(0)