petko_stankoski
petko_stankoski

Reputation: 10723

How to remove data-mce-style inside of an element, using jQuery?

$(editor[i])[0].outerHTML has a value of:

 <p style="color: red;" data-mce-style="color: red;">some string</p>

I want data-mce-style="color: red;" to disappear.
I'm doing that like this:

$(editor[i])[0].outerHTML.replace('data-mce-style="color: red;"', '');

But it's not replacing it.

Upvotes: 6

Views: 15413

Answers (6)

Abhishek Jain
Abhishek Jain

Reputation: 27

you need to change/remove a particular attribute, for that you need to use

$(editor[i]).removeAttr('data-mce-style');

for more info check the following links: http://api.jquery.com/removeAttr/

if you need to change the value of a particular attribute then do:

attr(attributeName, value);

for more info about the same check the following link: http://api.jquery.com/attr/

Upvotes: 0

apsillers
apsillers

Reputation: 116040

.replace creates a new transformed string; it does not alter the original variable. You're simply creating a new string and not storing the new string back into outerHTML, like:

$(editor[i])[0].outerHTML = $(editor[i])[0].outerHTML.replace('data-mce-style="color: red;"', '');

However, this only solves your immediate problem -- there are vastly better ways to accomplish what you need than stringifying and re-parsing your <p> element. Since you're using jQuery, the most obvious way would be to use the removeAttr method:

$(editor[i]).removeAttr('data-mce-style')​;​

Upvotes: 9

adeneo
adeneo

Reputation: 318352

$(editor[i]).removeAttr('data-mce-style')​;​

FIDDLE

Upvotes: 1

James Gaunt
James Gaunt

Reputation: 14793

Try:

$(editor[i]).removeAttr('data-mce-style')

http://api.jquery.com/removeAttr/

Of course this will apply to all elements in your selector. If you just want to apply this to element 0 then use:

$(editor[i]).first().removeAttr('data-mce-style')

Upvotes: 2

oxygen
oxygen

Reputation: 6049

element.setAttribute(attr, null) or element.removeAttribute

No need for outerHTML and replace. Note that replacing HTML will remove event listeners (other than attribute event handlers).

Upvotes: 1

Florent
Florent

Reputation: 12420

Try to use jQuery removeData():

$(editor[i]).removeData('mce-style');

Upvotes: 0

Related Questions