Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax append load

Tags:

ajax

append

load

it must be jquery

I have file text.html with 6 div in (a,b,c,d,e,f)

In another file i have a div, i like it to populate the content of a+b+c+d+e+f into that single div

I have try .load = but b remplace a i have try append, but i need a temp var

so now i am stuck

That code get the content from the file textes.html ... div #a and put the content into div #right, but the second libe REMPLACE the content of right a with right b

I like to append the content a + b NOT a over b

$(document).ready(function(){
var temp = load('textes.html #nicolas');
$('#right').append(temp);
var temp = load('textes.html #antoine');
$('#right').append(temp);
.
.
.
.

return false;
});

that code is the idea behind what should work, but i cannot make a ajax .load() to load content into a variable to append the content to the div...

<script type="text/javascript">
$(document).ready(function(){    
$.ajax({
  url: "textes.html",
  cache: false,
  success: function(html){
    $("#right").append(html);
  }
});
});
</script>

That code load the WHOLE html file, i like to get only some selected DIV #

like image 948
menardmam Avatar asked May 14 '09 00:05

menardmam


People also ask

What is load in Ajax?

jQuery - AJAX load() Method The load() method loads data from a server and puts the returned data into the selected element.

How do you use .append in JS?

The Element.append() method inserts a set of Node objects or string objects after the last child of the Element . String objects are inserted as equivalent Text nodes. Differences from Node.appendChild() : Element.append() allows you to also append string objects, whereas Node.appendChild() only accepts Node objects.


2 Answers

$(document).ready(function(){    
    $.get("textes.html",function(data){
        $("#right").append($("#nicolas",data)).end().append($("#antoine",data));
    },'html');    
});
like image 147
Chad Grant Avatar answered Oct 12 '22 07:10

Chad Grant


I had a similar issue just now and I think I figured out a way to do what we want using the .load() function. It's not pretty but never mind ;)

First off, I added a "TempDiv" to my html with a "visibility:hidden" style.

<div id="TempDiv" style="visibility:hidden"></div>

Then you run the jQuery :

$(document).ready(function(){
        $('#TempDiv').load('textes.html #nicolas', function(){
            $('#right').append($('#TempDiv').html());
        });

    });

I'm not sure it's the best way !

PS : That is my first stackoverflow post ;)

like image 44
Jibou Avatar answered Oct 12 '22 07:10

Jibou