Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:15:46+00:00 2026-06-13T05:15:46+00:00

I have a text like <AnyTag>here is a sample test text and more text</AnyTag>.

  • 0

I have a text like

<AnyTag>here is a sample test text and more text</AnyTag>.

Now I am selecting “test” and clicking an italic button in my page and it creates a new element <span class="ItalicClass"> and it surroundContents(). This woks fine.

Now I have text like

<AnyTag>here is a <i>sample test text</i> and more text</AnyTag>.

Now if I select “test” it does the same but I would like to close the parent first and then insert the new element. I know the parentNode hence I can insert a new class in the span element. Same way I need to open the same parent after the end of new element. The the final line should be

<AnyTag>here is a <i>sample </i><span class="ClassB">test</span><i> text</i> and more text</AnyTag>

If I select the word “more” then

<AnyTag>here is a <i>sample test text</i> and <span class="ClassA">more</span> text</AnyTag>.

Appreciate any pointers in this regard.

Regards
Dominic

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:15:47+00:00Added an answer on June 13, 2026 at 5:15 am

    First, you’ll need to split the text node containing ‘sample test text’ at two indexes so that you have three side by side text nodes: ‘sample ‘, ‘test’, and ‘ text’. (Note the spaces in sample and text).

    Second, create a range that encompasses the start of the parent node to the end of ‘sample ‘ text node. Extract the contents.

    var range, frag;
    range = rangy.createRange();
    range.setStartBefore(parent); //your 'i' tag
    range.setEndBefore(textNode); //your text node 'sample '
    frag = range.extractContents(); //extract a document fragment - removes items from dom
    parent.parentNode.insertBefore(frag, parent); //place the fragment before the parent node
    

    Repeat those same steps, this time creating a range from the start of the text node ‘ text’ to the end of the parent.

    var range, frag;
    range = rangy.createRange();
    range.setStartAfter(textNode); //your text node ' text'
    range.setEndAfter(parent); //your 'i' tag
    frag = range.extractContents(); //extract a document fragment - removes items from dom
    parent.parentNode.insertBefore(frag, parent.nextSibling); //place the fragment after the parent node
    

    Finally, the unwrap the contents of the original parent node ‘i’ and re-wrap it with your new tag.

    If any of this isn’t clear, let me know. I’ve been building a WYSIWYG editor and have tested this idea thoroughly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have a text like: this is a test :rep more text more more
I have a text like: test 1 test 2 test 3 and i want
If I have text like this; <p>&nbsp;</p> <p>this is the real text</p> <p>&nbsp;</p> <p>more</p>
I have text like this: 00:00 stuff 00:01 more stuff multi line and going
ok imagine that we have text like this String s=123 45678 91011122314 1516; now
The file under test will have text patterns like this: name1: function(){ }, name2:
I have text like this format term: 156^^^:^^59 datainput Or term: 156^^^:59 datainput or
i have text like this div bla-bla end div i need to get only
I have text documents like the following which contain single and multiple variables: title::
I have a text like this. Mr John Smith , Mr James Smith +(21)-(21)-12345678,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.