Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not in DOM after $.load() with jQuery?

Tags:

jquery

dom

load

var id = 'test';
var dom = $('#loader').clone().load("Views/chatBox.html");
dom.find('span.bn').text(id);

in chatBox.html,there is :

...
<span class="bn">AAA</span>
...

I want to replace "AAA" with "test",but failed(dom.find can't fetch it),which mean it's not available instantly.

How to do it the right way?

like image 998
omg Avatar asked Sep 11 '09 06:09

omg


People also ask

How do you check DOM is loaded or not?

It could be by placing a <script> tag (in which case the traditional onload or DOM-readiness solutions will work); or it could be loaded via AJAX or some other means, much later after the DOM is already loaded (so the previously mentioned solutions will never fire).

What is the load () method used for in jQuery?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.

Does jQuery manipulate the DOM?

Projects In JavaScript & JQueryjQuery provides a number of methods to manipulate DOM in efficient way. You do not need to write big and complex code to set or get the content of any HTML element.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

If you intend to work with the returned elements, you should do it on the callback function, that is because the retrieving of the HTML is done asynchronously, and the callback function executes when the request has ended and the elements are injected to the DOM :

var id = 'test';
$('#loader').load("Views/chatBox.html", function () {
  $('span.bn', this).text(id);
});

Also note that in your example, you were clonning the #loader element, and the cloned element is not in the DOM yet, you will have to insert it, but I'm not sure if you are really wanting to clone the element...

like image 141
Christian C. Salvadó Avatar answered Sep 21 '22 11:09

Christian C. Salvadó