1

im trying to read the contents of myDiv which resides in a separate html file and do some appending in the current page,is this possible? i know there is load(), but it displays the whole html as it is.

$( ".myDiv" ).each(function(){

//do stuff

});

i have tried $('#result').load('ajax/test.html #container') route, but it loads the contents from #container in to the div with id #result.i want to read the contents of the div with id #container from ajax/test.html,make some changes then display it in the div with id #result... im trying to use the each function on a div that resides on another page,hope im making some sense

2
  • Can you elaborate on the "and do some appending" part? Commented Aug 3, 2010 at 15:16
  • @nick im trying to read the contents from multiple divs in another page and then build a <ul> list from the contents i have read Commented Aug 3, 2010 at 15:23

2 Answers 2

6

.load() can also load page fragments. Example call from the docs:

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

would just load the div with the id #container out of the test.html file, into the div with the id #result.

http://api.jquery.com/load/

update

To load & modify the content you need the underlaying .ajax() method, like

$.ajax({
   url:      'yourfile.html',
   type:     'GET',
   dataType: 'html',
   success:  function(data){
        var $content = $(data).find('.myDIV');
        if($content.length){
           $content.find('p').text('modified');

           $('#some_local_id').append($content);
        }
   }
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks 4 ur answer, i know it will load into the div with id result,i dont want to load it ,i just want to read the contents make some changes and then display it
Cool I didnt know you could do that! +1
0

You can use load on a non displayed element.

something like this (untested, although I did something like this once)

var a = document.createElement("div");
$(a).load("otherpage.html");

Then you can do whatever you want to the loaded html.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.