1

I need to remove nested <b></b> tags. See the generated css and you'll understand the problem:

<div class="highlight" contenteditable="true">
    ahuasdhuasdhuasdhu 
    <b>
       <b>#</b>
       huadshuashud
    </b>
</div>

I want remove the tags that have a '# inside, but I don't want remove the '#'. (Note the hashtag can be any another value). For this, I wrote the following code:

// Highlight already been declared and just is a simple $('.highlight').
highlight.children('b').children('b').contents().unwrap();

Now I have the following code:

<div class="highlight" contenteditable="true">
    ahuasdhuasdhuasdhu 
    <b>
        "#"
        "huadshuashud"
    </b>
</div>

I want join this two strings, because when I double click it, it just select "#" or "huadshuashud", but I want that all content inside the tag is selected.

Have some way to improve my method or concatenate these two strings?

6
  • So I presume the "" are literally in the HTML? Commented Oct 9, 2013 at 2:22
  • Yes, they're literally in the HTML :/ Commented Oct 9, 2013 at 2:29
  • 1
    "because when I double click it, it just select "#"" Ah, so the actual problem. The default behaviour, in chrome anyway, is to select in this way. Try it by selecting this text: "#abc123". You'd need to create extra JS that looks for those click selections and "fix" the browser-specific behaviour. Not fun. Commented Oct 9, 2013 at 2:32
  • Thanks @Hamish, this is exactly my problem and is what I reported in comment of "eclanrs" answer. Anyway, thanks :) Commented Oct 9, 2013 at 2:34
  • If you want post a answer to my question, I will give you best answer. You don't tell me how to do that, but you told me that is almost impossible :P Commented Oct 9, 2013 at 2:34

2 Answers 2

1

Try:

$('.highlight > b').html(function(){
  return $(this).text();
});

Which will give you this:

<b>
  #
  huadshuashud
</b>
Sign up to request clarification or add additional context in comments.

2 Comments

You're rigth eclanrs, your answer give me "<b>#heuehueheu</b>", but I don't know, when I double click it I just get "#" or "heuehueheu" selected. This's a browser issue?
That's just the way it is, try any other symbol, and dbl-click will just select the word characters, it's not an issue. Try dbl-click on this string "#asdasdasd".
1

Selecting text in an element (akin to highlighting with your mouse)

Here you have a ready function. Edit the beginning to be able to pass an object instead of id and add a handler:

$(".highlight").ondblclick(function(){
    SelectText(this)
})

function SelectText(elementObject) {
    // here choose function you like and replace text variable
    text = elementObject;
    ...

Comments

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.