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 9043489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:44:35+00:00 2026-06-16T10:44:35+00:00

I’m using document.execCommand(unlink, false, false); to remove hyperlinks and anchors in my own html

  • 0

I’m using document.execCommand("unlink", false, false); to remove hyperlinks and anchors in my own html editor. It works fine except in one case: it doesn’t remove anchors in Firefox. Is this not supported in Firefox or is there another way to solve this?

Here’s my example:

<div contenteditable="true">This is an <a name="anker">anchor</a>. And this is a <a href="#">normal hyperlink</a>. Please try to select them and then press the "Unlink" button.</div>

<input type="button" id="btn" value="Unlink">

$(document).ready(function() {    
    $('#btn').click(function() {
        document.execCommand("unlink", false, false);
    });
});

Check it out on Fiddle

  • 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-16T10:44:36+00:00Added an answer on June 16, 2026 at 10:44 am

    To answer your question about what alternatives there are, you could force the issue and set any a element’s href attribute before calling unlink:

    $(document).ready(function() {    
        var $editable = $('[contenteditable]');
    
        $('#btn').click(function() {
            $editable.find('a:not([href])').attr('href', '#');
            document.execCommand("unlink", false, false);
        });
    });
    

    http://jsfiddle.net/Bz7pR/7/

    There are, of course, probably multiple ways to “solve” the problem. I figure $.unwrap() would probably work as well. I’m not that versed on document.execCommand and rich text editors, but I imagine you need to be careful, precise, and test test test.

    Note, that is just a demonstration; you’ve really got to think about that and consider exactly how you’re going to handle that problem. For instance, you could detect and only run that if Firefox is the browser, which would limit any unexpected damage or outcomes you might have.

    EDIT

    And here is a more complete version that only affects the selected text range:

    $(document).ready(function() {    
        $('#btn').click(function unlink() {
            var holder,
                frag,
                range,
                child;
    
            if (window.getSelection && window.getSelection().getRangeAt) {
                holder = document.createElement('div');
                frag = document.createDocumentFragment();
                range = window.getSelection().getRangeAt(0);
    
                $(holder)
                    .append(range.cloneContents())
                    .find('a:not([href])')
                    .attr('href', '#');
    
                while ((child = holder.firstChild)) {
                    frag.appendChild(child);
                }
    
                range.deleteContents();
                range.insertNode(frag);
            }
    
            document.execCommand("unlink", false, false);
        });    
    });
    

    http://jsfiddle.net/Bz7pR/12/

    I’m no genius when it comes to text ranges and selections, so I modified the code found in this answer:

    https://stackoverflow.com/a/6252893/451969

    To come up with what is above. If someone sees something that’s either wrong or incoherent, leave me a comment below.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I am using JSon response to parse title,date content and thumbnail images and place
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.