Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove <br> from specific div

Tags:

jquery

I need to remove break from a specific div, i know how to remove all breaks with

$('br').remove();​

But I need to keep some breaks elsewhere on the page ,

How would I remove the breaks from a div with the id "grape1" and "grape12" but leave all others intact?

like image 674
user1450749 Avatar asked Jun 28 '12 03:06

user1450749


3 Answers

$('#grape1,#grape12').find('br').remove();
like image 80
xdazz Avatar answered Oct 20 '22 14:10

xdazz


$('br', '#grape1, #grape12').remove();​

The second argument in the $ selector allows you to filter your search to a given subset, instead of the entire DOM. In this case we tell jQuery to only look inside the grape1 and grape12 divs. This will match all br's in those div's.

JsFiddle

like image 42
Brombomb Avatar answered Oct 20 '22 15:10

Brombomb


If that doesn't work (as happened in my case also) it's probably because the <br> are generated by some scripts after the JS .remove() code execution.

To check if I was right I set a quite long timeout and all my unwanted <br> disappeared after that time. So, in the end I just decided to put a simple CSS

#grape1 br, #grape12 br { display: none; }

and eveybody's happy =)

like image 2
Sgaddo Avatar answered Oct 20 '22 15:10

Sgaddo