Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery load() and append

Tags:

jquery

silly quick question:

I have my:

$('#result').load('ajax/test.html'); 

but what if I don't want to insert my loaded content into #result, but prepend it to #result, maintaining all precedent elements? Is it possible to create a variable, load it with content and then append or prepend it to my #result? I imagine some other scenarios where with my brand new variable I can manipulate it before inserting it into the DOM.

like image 866
Daniele Avatar asked Jan 10 '12 17:01

Daniele


People also ask

How to append data with jQuery?

jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.

Why use append in jQuery?

In jQuery, the append() method is used to insert specified content as the last child (at the end of) the selected elements in the jQuery collection.

How to use append in ajax?

click(function() { if(pageNum <= max) { $. get(nextLink + ' article', function(data) { $('#content'). append(data); }); } }); If filtering specific elements with load(), you will have to do that yourself.

What is load 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.


1 Answers

You mean something like this?

var content; $.get('ajax/test.html', function(data){     content= data;     $('#result').prepend(content); }); 

That saves your loaded content into a variable first and you can manipulate it however you want.

like image 60
Sven Bieder Avatar answered Oct 07 '22 16:10

Sven Bieder