user
user

Reputation: 545

Remove style attributes from string

I'm trying to remove all style attributes from a string (and not an object), in JavaScript or jQuery.

Example

var html = "<p style="color:red">my text</p>";

And I want to obtain this:

<p>my text</p>

Is it possible to do it without calling the functions that we can call on objects (as removeAttr)?

Thanks

Upvotes: 3

Views: 8637

Answers (2)

Monkey_Dev1400
Monkey_Dev1400

Reputation: 944

See if this helps.

$("button").click(function() {
  $("p").removeAttr("style");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<p style="color:red">my text</p>

<button>Remove the style attribute from all p elements</button>

Upvotes: -1

user8086575
user8086575

Reputation:

If you are not looking to use JavaScript or jQuery objects, have you tried using regular expressions?

This answer to a previous question shows a php version that can be adapted to JavaScript using the replace function.

Upvotes: 5

Related Questions