How to detect if selected text is wrapped in span tags

cosmo

I have a simple contenteditable div that serves an editor, and a button with an onclick function that changes the color of selected text to be yellow.

    function yellow(){
    {       
        var selection = window.getSelection().getRangeAt(0);
        var selectedText = selection.extractContents();
        var span = document.createElement("span");
        span.style.color = "yellow";
        span.appendChild(selectedText);
        selection.insertNode(span);
    }
    }

I recently noticed that if I selected text OUTSIDE of my div, it could still be colored yellow (since I'm using window.getSelection()).

To fix this I added an if statement:

    function yellow(){
    {       
        var selection = window.getSelection().getRangeAt(0);
        if(window.getSelection().baseNode.parentNode.id != "editor1") return;
        var selectedText = selection.extractContents();
        var span = document.createElement("span");
        span.style.color = "yellow";
        span.appendChild(selectedText);
        selection.insertNode(span);
    }
    }

I'm looking to add another if statement similar to this:

if(window.getSelection().baseNode.parentNode.id != "editor1") return;

except instead of id != "editor1", it detects whether the selected text has span text wrapped around it or not.

nsmarks

if (window.getSelection().baseNode.parentNode.tagName === 'SPAN')

https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to detect that a text is wrapped in a label?

From Dev

How can I detect text selected in a div?

From Dev

How can I detect text selected in a div?

From Dev

Wrapping text in span tags

From Dev

Wrap with span if text is selected

From Dev

Text indent a wrapped inline-block span

From Dev

Detect hover on selected text

From Dev

Remove <span> tags, but keep the text?

From Dev

Match text not inside span tags

From Dev

Change span text with selected option

From Dev

Change span text with selected option

From Dev

How to get the actual text from a span element enclosed within multiple span tags, using Selenium Webdriver with Java

From Dev

How to detect text selected by double clicking using Firefox SDK (for addons)?

From Dev

How To Detect If Web App Is Wrapped By CocoonJS

From Dev

How To Detect If Web App Is Wrapped By CocoonJS

From Dev

Detect when text is selected iOS

From Dev

How to set a background for wrapped text?

From Dev

Getting text from React wrapped span using webdriverio

From Dev

Determine "tags" of selected text in ExtendScript

From Dev

Get the tags of selected text in JTextPane

From Dev

Determine "tags" of selected text in ExtendScript

From Dev

How to replace font tags with span?

From Dev

In iframe how to "unwrap" span tags

From Dev

In iframe how to "unwrap" span tags

From Dev

Find text in element that is NOT wrapped in html tags and wrap it with <p>

From Dev

Misaka template tags for Django - text wrapped in double quotes

From Dev

jquery surround selected text and image with span tag

From Dev

Text editor with option to enclose selected text in tags

From Dev

Nested rounded border graphic over text within <span></span> tags?

Related Related

HotTag

Archive