tags:

views:

71

answers:

4

I have the following markup which is just a small portion of the total markup.

<div align="center">
  <img src="v/vspfiles/templates/100/images/headings/heading_shoppingcart.gif">
</div>
<br><br>

I would like to remove the two <br> tags.

Note: there are other <br> tags on the page both before this and after this that I do not want to removed.

I thought of using a selector to target the div by the src which contains heading_shoppingcart.gif and something like .after and then .remove the <br>.

Unsure of the correct syntax or if there is a better/easier way to do it.

+2  A: 

How about:

$("img[src$='heading_shoppingcart.gif']").parent().nextAll('br').remove()
  • The [$=] is the 'attribute ends with' selector.
  • .parent() moves up to the containing element
  • .nextAll() gets all following siblings
Bobby Jack
You typed faster :/
Alexander
Both this and the one by Alex worked fine but I like the selector in this example just a tad better
+1  A: 
$('[src~=images/headings/heading_shoppingcart.gif]').parent().nextAll('br').remove();
Alexander
A: 

I think the following would work:

var br1 = $("img[src='v/vspfiles/templates/100/images/headings/heading_shoppingcart.gif']").parent("div").next("br");

br1.add(br1.next("br")).remove();
Pandincus
+1  A: 

This will safely retain any subsequent <br> elements since you seemed to allude to the idea that there may be more that should be preserved.

$('img[src$=heading_shoppingcart.gif]').parent().nextUntil(':not(br)').remove();
patrick dw
This surprisingly did not work to remove the br's
@user - It works great for me here: http://jsfiddle.net/DuPx4/ What version of jQuery are you using? The `.nextUntil()` was added in `jQuery 1.4`. I'd highly recommend that you upgrade if needed and possible.
patrick dw
You must have made an edit cause your first code did not work but now it works. I think this is a more precise answer even though the others work as well. THX
@user - Nope, no edit. (At least none beyond the first few seconds after I posted.) Must've been a ghost in the machine. :o) Glad it worked.
patrick dw
I swear there was a $('div > img[src$=heading_shoppingcart.gif]') selector there a minute ago. Must be going nuts.
@user - Hmmm... There was originally, but I thought I got rid of that immediately. Maybe I'm remembering wrong. One main error I had was that I somehow had left out `.parent()`. You're probably right. :o)
patrick dw