I am currently having trouble with the following (here is some sample code first):
<div id="container"></div>
<script type="text/javascript">
$('#container').load('content.html');
$('.elementInContentHTML').fadeIn();
</script>
In short, I want to be able to access elements that have been dynamically added to a page without attaching them to event handlers.
I know about the live()
method, but I do not want to bind my action to any event, i.e. I just want to run some actions with these new elements without clicking them, focusing, blurring, etc.
The load() method loads data from a server and puts the returned data into the selected element.
The select() method is an inbuilt method in jQuery which is used when some letters or words are selected (or marked) in a text area or a text field. Syntax: $(selector). select(function);
The Load() method in jQuery helps to load data from server and returned into selected element without loading the whole page. Syntax: $(selector). load(URL, data, callback);
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
The load
function is asynchronous.
Your next line runs before the content is loaded.
You need to put your code inside the load
function's callback, so that it will only run after the new content is loaded:
$('#container').load('content.html', function() {
$('.elementInContentHTML').fadeIn();
});
You could try using the callback for when load completes? See http://api.jquery.com/load/
$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With