-1

I have found solution for get html of a div on another page using jQuery ajax in here, So I made this script, when mouseenter a link, that will get data using ajax :

index.html

<ul>
    <li><a class="basic" href="data1.html" rel="1" title="Accel World">Accel World</a>
    </li>
    <li><a class="basic" href="data2.html" rel="2" title="Ano Hi Mita Hana">Ano Hi Mita Hana</a>
    </li>
</ul>
<div id="content" class="isi" style="display:none">
<h1 class="title-content"></h1>
<div class="body-content"></div>
</div>

<script src="jquery.min.js"></script>
<script type="text/javascript">

$('a.basic')
.mouseenter(function() {
    var thisrel = $(this).attr('rel');
    var thistitle = $(this).attr('title');
    var thishref = $(this).attr('href');
    var content = $('#content');
    var idcontent = content.attr('id');
    var wadah = idcontent+'-'+thisrel;
    content.attr('id', wadah);
    var thattarget = $('#'+wadah);
    thattarget.show();
    thattarget.find('.title-content').text(thistitle);
    $.ajax({
        url: thishref,
        type:'GET',
        success: function(data){
             thattarget.find('.body-content').append($(data).find('#sct_content').html());
           console.log(thattarget.attr('id'));
        }
    })
  })
  .mouseleave(function() {
     var isi = $('.isi');
     var titleisi = $('.title-content').empty();
     var bodyisi = $('.body-content').empty();
     $(isi).attr('id', 'content');
     $(isi).hide();
  });
</script>

data1.html and data2.html

<!-- html stuff -->
 <div id="sct_content">
   <div class="article">data 1 <!-- data 2 for data2.html --></div>
 </div>
<!-- html stuff -->

And this logs on console when mouseenter on link :

> XHR finished loading: GET "http://localhost/getip/data1.html". jquery.min.js:4
  send jquery.min.js:4
  m.extend.ajax jquery.min.js:4
  (anonymous function) custom.html:34
  m.event.special.(anonymous function).handle jquery.min.js:3
  m.event.dispatch jquery.min.js:3
  r.handle
content-1

problem : The data not appear on .body-content, because selector $(data) is undefined

Community
  • 1
  • 1
GeekToL
  • 1,815
  • 1
  • 24
  • 46

2 Answers2

1

Remove ajax and Try This $(wadah).find('.body-content').load( thishref+" div#sct_content" );

Jithin Lukose
  • 344
  • 4
  • 18
0

Jithin's answer works fine but I have used that before ask. I found solution in here

The following jQuery Won't work:

$(data).find('#sct_content').html();

as the divs are top level elements and data isn't an element but a string, to make it work you need to use .filter

$(data).filter('#sct_content')
Community
  • 1
  • 1
GeekToL
  • 1,815
  • 1
  • 24
  • 46