views:

123

answers:

3

I'd like to remove all <br/> <br /> or <br> tags from #id p. I tried $('#id p').find('br').remove(); but it doesn't seem to work.

EDIT:

Ah, I was accessing the element by ID, and it was a CLASS, uh! :) So $('#id p').find('br').remove(); works fine!

A: 

Are you trying to search for BR tage within a paragraph with a particular ID? If so, I think that would be that would be p#id instead (I'm no JQuery expert, but it tends to follow CSS selectors).

richardtallent
No, p is 'inside' #id.
Nimbuz
+1  A: 

You could do this - however you would lose all events attached. It is generally not recommended.

// Get the HTML and remove <br /> variants
var htmlCleaned = $('#id p').html().replace(/<br\s?\/?>/, ''); 

// Set the cleaned HTML
$('#id p').html(htmlCleaned);

I don't see why your example above shouldn't work though...

It may also be that your jQuery selector is incorrect :P

alex
Ah, thanks for the heads-up, mine worked. I was accessing the element by ID, whereas it was a CLASS, uh! :)
Nimbuz
Ah! Don't worry I have a few questions on here like that...
alex
+2  A: 

I tried out this example, and it worked for me:

<html>
<head>
    <title>Test!</title>
    <script src="jquery-1.3.2.js"/>
</head>
<body>
    <div id="foobar">
     <p>
      This is <br/> some <br/> text.
     </p>
    </div>
    <input type="button" value="Click" onclick="$('#foobar p').find('br').remove();"/>
</body>
</html>

Is there anything different from what you are trying to do?

Wesley Petrowski
Could also do `$('#foobar p br').remove();` . Also, check out http://jsbin.com/ . It's awesome. Welcome.
Kobi