Neetu sharma
Neetu sharma

Reputation: 148

How to remove span tag using jquery without using remove method?

can you please tell me how to remove the span tag using jquery. INPUT

<span class="abc">PQR </span>

OUTPUT

PQR

I done before but don't remember I think I used regex or replace .:(

Second

How to replace &nbsp by a space(" ") ?

I used like that but not work.

replace(/&nbsp;/g,'');

Upvotes: 1

Views: 178

Answers (2)

user1234
user1234

Reputation: 8978

For the first part, you can use $('.abc').contents().unwrap()

For the second part see jods answer :)

Upvotes: 2

jods
jods

Reputation: 4591

For the first part, Tim's answer is great. $('.abd').contents().unwrap().

For the second part, please do it properly. &nbsp; is an html-encoded special characters (non-breakable space), but there are tons of other characters you can encounter (like &eacute; for é).

Unfortunately there's no built-in standard html decode function, but you can create one with jquery like this:

function htmlDecode(encodedText) {
  return $("<div>").html(encodedText).text();
}

Upvotes: 3

Related Questions