Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery remove html elements inside variable (jquery object)

Inside HTML I have this DIV:

<div class="teaser">
  <img src="thumb1.jpg"><img src="thumb2.jpg"><br>  // usually one thumb image; can be up to three
  some text goes here, sometimes not<br>
  <a href="example.html">always a link here</a><br>
  and sometimes another line here
</div>

I need to capture the content of the DIV minus the first line (the thumbs) in a variable to pass on as title (caption) to a lightbox script. However, in a day I've learned something about javascript & jquery (both are my weak spots), yet nothing I found by searching the web, including several threads here on stackoverflow, did it, either throwing errors or still including the thumbs or removing the text, too, or even removing the thumbs from the DOM:

// takes the whole
var teaser = $(".teaser");

// TypeError: $(...).html(...).find is not a function
var teaser = $(".teaser").find("img").remove();

// IMGs removed from DOM
var teaser = $(".teaser").find("img").remove();

// IMGs still included; I think I understand why
var teaser = $(".teaser").not("img");

// ditto
var teaser = $(".teaser").clone().not("img");

// ditto:
var teaser = $(".teaser");
$(teaser).find("img").remove();

// no teaser at all (everything removed, not just the IMGs)
var teaser = $(".teaser");
teaser = $(teaser).find("img").remove();

// ditto
var teaser = $(".teaser").clone().find("img").remove();

Changing the sequence in chainings didn't work either (though I may have missed one or another).

This workaround seems to be ok:

var teaser = $(".teaser").html().replace(/(<img.*?)+<br[^>]*?/i,"");

However, depending on regex implementation in browsers it might be shaky, so I'd rather have something more robust, particularly not getting the first line at all.

TIA for any suggestion - hope I could make myself clear.

Edit:

using clone() in conjunction with children() or contents() got me a step further. However, both these will also remove the first text node, leaving two empty BR behind:

$("div.teaser").each(function() {
  var teaser = $(this).clone().children().not("img");
  var teaser = $(this).clone().contents().not("img");
});

OTOH filter("img"), find("img") or not("img") followed by remove() removes everything but the IMGs, which is beyond my head except for not()

var teaser = $(this).clone().find("img").remove();

Edit 2:

To wrap things up:

This is the solution @karaxuna suggested. It is important to re-assign the variable.

var teaser = $(this).clone();
teaser.find("img,br:first").remove();
teaser = $.trim(teaser.html());

While doing some further reading to better understand the logic behind the failures and the final solution I got a clue for doing it slightly more jquerish:

var teaser = $(this).clone();
teaser = $.trim($("img,br:first",teaser).remove().end().html());

The clue is the scope, or context, added to the $() selector. Using this here is familiar enough, yet it never occurred to me to use the var. Also, end() will keep remove()from still going all the way to the DOM...

$.trim technically not necessary of course, HTML ignores empty lines anyway.

So including the above workaround with regex there are at least three ways to go - kinda reminds me of Perl ;)

Thanks to both @karaxuna and @Ravi for sharing their experience and ideas!

like image 889
JayAitch Avatar asked Mar 07 '13 11:03

JayAitch


People also ask

How can you remove and HTML element using jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

How do I remove an innerHTML element?

innerHTML property innerHTML property will get or set the HTML markup contained within the element. But you can utilize this property to “remove” any elements within the container, by setting the value to an empty string. This property is fully cross-browser, read full docs on innerHTML.

What jQuery method is used to completely remove attributes from elements?

The removeAttr() method removes one or more attributes from the selected elements.

How do you delete something in jQuery?

remove() method takes elements out of the DOM. Use . remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.


3 Answers

No need for html()

var teaser = $(".teaser").find("img").remove();

EDIT:

Try this:

var teaser = $(".teaser").clone();
teaser.find("img").remove()
// use teaser
like image 195
karaxuna Avatar answered Oct 21 '22 09:10

karaxuna


if You want to remove the whole html. then you can use empty()method

$(".teaser").empty();

if you are removing image only. then

$(".teaser").find("img").remove();
like image 23
Ravi Gadag Avatar answered Oct 21 '22 09:10

Ravi Gadag


It's late, but try this $('img',$('.teaser')).remove().end().clone()

like image 35
janbee Avatar answered Oct 21 '22 08:10

janbee