harry
harry

Reputation: 43

How do I strip the p tags with jQuery?

<p>
     <a href="link"><img src="image"></a>
</p>

Expected result: <a href="link"><img src="image"></a>

Upvotes: 3

Views: 2806

Answers (6)

R1234
R1234

Reputation: 484

give an id to it.

<p id="test">

<a href="link"></a>

</p>

then use

var link = $("#test")[0] ; 

Upvotes: 0

Sylvain
Sylvain

Reputation: 3873

You could also use unwrap():

$("p").contents().unwrap();

jsFiddle example

Upvotes: 3

verdesmarald
verdesmarald

Reputation: 11866

You can use replaceWith() to replace an element with its contents:

$('p').replaceWith
(
    function() { return $(this).contents(); }
);

See here.

Upvotes: 12

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

Give an Id to your p tag

<p id='id'>
     <a href="link"><img src="image"></a>
</p>

then use Jquery to select inner HTML

$('#id').html();

Upvotes: 0

Joshua - Pendo
Joshua - Pendo

Reputation: 4371

Something like:

$("p").each(function(){
    var content = $(this).html();
    $(this).parent().append(content);
    $(this).remove();
});

searches all p-tags, gets content. Appends it to the parent element and removes the p tag. Does not work in all cases (for example if the P tags should be at the start of the parent element.)

Upvotes: 0

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

For a particular P element, you can just go

$('p').html();

Upvotes: 0

Related Questions