0

I need to exclude some divs (one or several) from the page that I .load() from the same domain to modal, I use this to load pages:

$(document).ready( function() {
$("#div-id-where-to-load-page").load("loaded-page-url");});

I tried something like this:

$(document).ready( function() {
$("#div-id-where-to-load-page").load("loaded-page-url").remove(#div-id-to-remove);});

and its not working, if I break it to separate functions it doesn't work either, I am new to JS, what am I doing wrong?

All scripts are loaded from the separate file (if it is important).

  • https://stackoverflow.com/questions/3015103/jquery-exclude-elements-with-certain-class-in-selector this will help you – Dhiren Aug 27 '18 at 11:50

1 Answers1

0

You can have a callback function on the .load() method. This way you guarantee that the elements you want to remove are already loaded to the DOM when you call the .remove().

$(document).ready( function() {
    $("#div-id-where-to-load-page")
        .load("loaded-page-url", function() {
            $('#div-id-to-remove').remove();
        });
});
Pedro Henrique
  • 601
  • 6
  • 17
  • What If I want to exclude several divs, I just add this line " $('#div-id-to-remove').remove(); " again? Thank You, it works like a charm! @Pedro Henrique –  Aug 27 '18 at 12:49
  • You can call once for each element you want to remove, but `.remove()` will also work on a list of elements, something like this `$('p').remove();` will remove all the paragraphs. – Pedro Henrique Aug 27 '18 at 17:26