1

I'm using JSON to receive content from a Wordpress website and display it on my mobile app. The JSON response I'm receiving has some inline styles within it that I don't need. I do however like to keep the actual html structure. Is this something that I can do with Javascript/jQuery?

For example, I like this:

<span style="color: red;">some text</span>
<p style="text-align: right;">Other stuff</p>

Turn to this:

<span>some text</span>
<p>other stuff</p>
1
  • 2
    Not sure I get it, your JSON looks a lot like HTML to me? And then you could just remove the style attribute? Commented Nov 29, 2012 at 15:52

2 Answers 2

4

Create a jQuery object from your HTML string and then use .removeAttr() as others have said:

var $result = $(yourHTMLString);

$result.find('[style]').removeAttr('style');
Sign up to request clarification or add additional context in comments.

2 Comments

Don't you mean .removeAttr('style');?
Awesome, learning something new everyday. I can use it as $result[0] after stripping all the inline styles. Thanks
0

You can use the removeAttr() method to remove attributes, try this:

$("span").removeAttr('style');

Update

My JSON response is just text, it's not a DOM element that I can use removeAttr on

In that case you can convert the response text to a jQuery object and run the removeAttr() on that:

var $html = $(jsonResponseData);
$html.find('span').removeAttr('style');

1 Comment

My JSON response is just text, it's not a DOM element that I can use removeAttr on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.