load content after an element

Asked

Viewed 1,136 times

2

I am trying to make a function to load a content with .load() of jQuery, the .load() receives a parameter param which would be the archive address.
And I’d like that html was loaded after a specific element in the aside.
I tried using the method .after() but I couldn’t, I wonder if anyone has any solution?!

And I have another question, after uploading this new content, whether the methods that are within the $(document).ready(); will still work, to interact with the new loaded elements?!

HTML line where the onclick event is

<li class="list-group-item pointer" onclick="getTemplate('admin/email/lista-emails.php')">
  Lista de E-mails
  <span class="indicador success white right">14</span>
</li>

Javascript code

function getTemplate(string){
  $("#here").after().load(string);
}

Note: He loaded the content into the aside

  • Which error is displayed in the console?

  • Sorry, I forgot to inform... He uploaded the content inside the aside

3 answers

2

You can use $.get() to fetch the content and insertAfter() to position it, instead of load() and after();

function getTemplate(string){
  var html = $.get( string );
  $( html ).insertAfter('#here');
}

  • I managed to bring through an ajax call, which I did, but by using this method that insertAfter, he returned me that mistake: Uncaught TypeError: Cannot read property 'ownerDocument' of undefined

  • when you call $.get( string ) it has to return a well formatted html, otherwise it may give this error

  • And how would I get this html from the returned object and insert after the #here, it brings the html in responseText

0


I was able to solve it this way:

function getTemplate(string){
  $.ajax({
    type     : 'GET',
    url      : string,
    dataType : 'html',
    success  : function(data){
      //remove o conteudo caso já tenha carregado
      //e evita carregar multiplas vezes o mesmo conteudo
      $("#loadContent").remove(); 
      //carrega o html após o elemento ASIDE que resultou na consulta.
      $('aside').after(data);
    }
  });
}
  • If an answer has been resolved, mark with the green V next to it (even if it is your own) to mark as solved, instead of editing the title.

0

Could be done using $.getJSON done();

Thus:

funtion getTemplate(parametro){
  $.getJSON("url", data : {paramentronome : paramento }).
   .done(function (dado ) {
      $("#div").html(dado);
   });
}

That helps?

  • Vlw but through his answers I mounted an ajax call, now it remains to insert the content after the tag #here

  • Your #here tag, do you want to add content to it? That’s it, I didn’t quite understand what that tag would be and what would be done with it ... ;$("#here").html("texto"); // adiciona um html novo $("#here").append("texto"); // adiciona um texto ao que ja existia

  • Vlw already solved, wanted to load the contents after the tag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.