3

I have a contenteditable area and when the user has finished editing I will save the data to a file. When a user uses first one browser and then another the different styles created by the contenteditables leads to messy and incompatible code.

I was wondering if there was any way to replace the <span style="font-weight:bold">XXX</span> tags created in Chrome with 'standard' tags such as <b>xxx</b>.

Thanks in advance

2 Answers 2

4

you can try this:

$('span').each(function(){ // iterate to the all the spans
    if($(this).css('font-weight') == 'bold'){  // check if font is bold
       $(this).contents().unwrap().wrap('<b></b>');​ 
    }  // unwrap the content and wrap it
});

So, what going on here is:

  1. Iterate through all the spans in your document
  2. check if the css style is "bold"
  3. if true then first unwrap the contents it holds and wrap it with the tags of your choice

so similarly you have to check for all other css styles separately and replace/wrap it accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, will require a new if for each command, but should be fine.
1

Use:

$('span').each(function(){ // iterate to the all the spans
  if($(this).css("font-weight") == "bold"){  //
  $(this).contents().unwrap().wrap('<b></b>');​
 }
});

Working Demo

2 Comments

I need to replace the bold with b tags, the underline with u etc. I don't see how this code can discern between these differences
@StorySystems: did not read that part. Updated code and demo now.

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.