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

  • Home
  • SEARCH
  • 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 6684971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:00:09+00:00 2026-05-26T05:00:09+00:00

Le Code: http://jsfiddle.net/frf7w/12/ So right now, the current method will take the selected text

  • 0

Le Code:
http://jsfiddle.net/frf7w/12/

So right now, the current method will take the selected text exactly as… selected, and add tags so that when it is displayed, the page doesn’t blow up.

But what I want to do:
Is to, when a user selects a portion of a page, if there are un-matched tags within the selection, the selection will either jump forward or backward (depending on what unmatched tag is in the selection) to the tag(s) that make the selection valid html.

The reason why I want to do this, is because I want a user to be able te select text on a page, and be able to edit that text in a WYSIWYG editor (I can currently do this with the linked code), and then put what they’ve edited back into the page (currently can’t do this, because the method I use adds tags).

  • 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-05-26T05:00:10+00:00Added an answer on May 26, 2026 at 5:00 am

    You can change the boundaries of the selection by adding a range:

    var sel = window.getSelection(),
        range = sel.getRangeAt(0);
    
    var startEl = sel.anchorNode;
    if (startEl != range.commonAncestorContainer) {
        while (startEl.parentNode != range.commonAncestorContainer) {
            startEl = startEl.parentNode;
        }
    }
    var endEl = sel.focusNode;
    if (endEl != range.commonAncestorContainer) {
        while (endEl.parentNode != range.commonAncestorContainer) {
            endEl = endEl.parentNode;
        }
    }
    
    range.setStartBefore(startEl);
    range.setEndAfter(endEl);
    
    sel.addRange(range);
    

    The above example will give you a selection that is expanded to cover the entire of the tree between the start and end nodes, inclusive (thanks to commonAncestorContainer()).

    This treats text nodes as equal to dom elements, but this shouldn’t be a problem for you.

    Demo: http://jsfiddle.net/Nq6hr/2/

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

Sidebar

Related Questions

code: http://jsfiddle.net/byHMP/2/ (note: when properly selected, the code will insert a span around the
Check this code: http://jsfiddle.net/ZXN4S/1/ HTML: <div class=input> <input type=text size=50 id=test_input> <input type=submit value=send
Please take a look at this simple code: http://jsfiddle.net/kerp3/ The box has an inner
I have this code: http://jsfiddle.net/5RbrL/ As you can see, the text doesn't go over
I have the following code: http://jsfiddle.net/YfzbZ/ HTML <dl class=dropdown right> <dt><a>Options</a></dt> <dd> <ul style=display:none;>
code: http://jsfiddle.net/WRppV/4/ I should get something like content=> [141, [203, 206, 204, 205], 142,
I am using this code: http://jsfiddle.net/q3nUS/ $('#cf11_field_20').change(function() { $(#li-11-22)[$(this).val() == full03_accommodation_hotel ? 'show' :
Here is my code http://jsfiddle.net/AB6J3/7/ you can see, edit, etc. When I roll over
I have the following code: http://jsfiddle.net/SPWWx/ I'm completely new to javascript, this is my
I need help concerning following code: http://jsfiddle.net/8PsdE/ <SELECT ID=pizzasize NAME=pizzasize> <OPTION VALUE=s>small <OPTION VALUE=m>medium

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.