Use the remove method
This would work if the browser allowed you to put a block element such as
table
inside a
p
:
var p_el = $("<p>testing<table><tr><td>cell 1</td></tr></table> a table</p>")
# Note: This gets slit into 3 elements: p, table, p. This would not
# happen with a table inside a block element like a `div`
# Find all table elements and remove them:
p_el.find('table').remove()
# If you want the string back you can do:
p_el[0].outerHTML # p_el[0] gets the DOM object from the jQuery object
# If the string has already been rendered, then of course you should use the
# appropriate selector to find the `table` elements within `p` elements and then remove them:
$('.someParentEl p table').remove()
# If you need to remove multiple types of elements, then a more elaborate
# selector can be used within remove. I recommend reading up on jQuery selectors
# But an example might be to exclude div tags as well:
$('.someParentEl p').find('table, div').remove()
# What I suspect is that you actually want to remove all block elements from the string
# (table, div, etc. leaving b, i, strong, span, etc.):
$('.someParentEl p *').filter(function(index) {return $(this).css("display") === 'block'}).remove()
# Note: This will only work on elements already in the DOM.
Related: Why is <table> not allowed inside <p>
And: Nesting block level elements inside the <p> tag... right or wrong?
If you are indeed dealing with tables inside paragraphs in your DOM
then this is a whole another can of worms, and I'm not sure how to consistently clean up that mess on the client side.
If the p
is just an unfortunate choice in your example, then all my examples should work with the p
replaced.