1

I want to delete all the elements having an iframe as a child. For example, if I want to delete all the DIVs without an iframe as a child:

$("div :not(iframe)").remove();

But it doesn't work... What would be the correct instruction?

Thank you very much!

Ortzi
  • 363
  • 1
  • 6
  • 17

3 Answers3

1

You can use combination of :not and :has

Live Demo

$("div:not(:has(iframe))").remove();
Adil
  • 146,340
  • 25
  • 209
  • 204
  • @Royi Namir: Why? It's an element, not a string. – BoltClock Jul 18 '13 at 13:18
  • @user2595583: Yeah it's strange, but I posted a question exploring that - http://stackoverflow.com/questions/12475595/why-do-the-not-and-has-selectors-allow-quoted-arguments – BoltClock Jul 18 '13 at 13:19
  • @BoltClock according to the documentation (http://api.jquery.com/has/#has-selector) the argument should be a string. just like you do $("iframe") and not $(iframe) – Royi Namir Jul 18 '13 at 13:21
  • 1
    @Royi Namir: Ah, you're confusing the [`.has()` method](http://api.jquery.com/has) with the [`:has()` selector](http://api.jquery.com/has-selector). – BoltClock Jul 18 '13 at 13:24
0

I want to delete all the elements having an iframe as a child

I always use a filter:

$('div').filter(function(i,el) {
    return $(el).find('iframe').length;
}).remove();

to do the opposite:

$('div').filter(function(i,el) {
    return !$(el).find('iframe').length;
}).remove();
adeneo
  • 312,895
  • 29
  • 395
  • 388
0
var $divsWithoutIframeChilds = $('div').filter(function(i, el){
    return $(el).find('iframe').length < 1;
});

$divsWithoutIframeChilds.remove();
Johan
  • 35,120
  • 54
  • 178
  • 293