?
Solved

Javascript get font style from selected text

Posted on 2009-12-30
6
Medium Priority
?
1,074 Views
Last Modified: 2012-05-08
Hi Experts!

I have below code.
When I select any text on my website and after click special button with funcion wog_qq -
the selected text has been inserted to my textbox.

Ok, but I want copy also font style.
I.E.:
a) If text is bolded, function should detect it and insert [B] [/B] tags.
b) If text is red, function should detect it and insert [COLOR=RED] or [COLOR=#FF0000]
c)If text is red and bolded, function should detect it and insert [B][COLOR=RED]str[/B][/COLOR]

How do it?
<script type="text/javascript">
	<!--
	var textbox = document.getElementById("textbox");
	function wog_qq()
	{
		if (window.getSelection)
		{
			var str = window.getSelection().toString(); 
		}
		else if (document.getSelection) 
		{
			var str = document.getSelection();
		}
		else if (document.selection) 
		{
			var str = document.selection.createRange().text;
		}
		else if (document.selection && document.selection.createRange)
		{
		var range = document.selection.createRange();
		var str = range.text;
		} 
		else
		{
			var str = "error";
		}
	textbox.insert_text(str, false);
	}
	// -->
</script>

Open in new window

0
Comment
Question by:WoG
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26152384
I think you only need to find out the wrapper element of the selected text from where you will find out the style of that particular element.
ie, at the onselect event you will try to

<script>
  onselect = checkStyle(event);
 
  function checkStyle(event)
  {
      var element = event.target;
      alert( element.style.font );
  }
</script>
0
 

Author Comment

by:WoG
ID: 26153064
Error: 'style' is empty or it isn't a object.
<script type="text/javascript">
        <!--

function wog_qq()
        {
                if (window.getSelection)
                {
                        var str = window.getSelection().toString();
                        //alert( element.style.font ); 
                }
                else if (document.getSelection) 
                {
                        var str = document.getSelection();
                }
                else if (document.selection) 
                {
                        var str = document.selection.createRange().text;
                }
                else if (document.selection && document.selection.createRange)
                {
                var range = document.selection.createRange();
                var str = range.text;
                } 
                else
                {
                        var str = "error";
                }
                var element = str.target;
      			alert(element.style.font);
        	}  			
  			// -->
</script>

Open in new window

0
 
LVL 4

Expert Comment

by:jake_0011
ID: 26153984
You need to move your "var textbox = ..."  INSIDE the function wog_qq(){..}  
Otherwise when you call wog_qq textbox is always null.


<html>
<head>
<script type="text/javascript">
	<!--
	
	function wog_qq()
	{
	var textbox = document.getElementById("textbox");
		if (window.getSelection)
		{
			var str = window.getSelection().toString(); 
		}
		else if (document.getSelection) 
		{
			var str = document.getSelection();
		}
		else if (document.selection) 
		{
			var str = document.selection.createRange().text;
		}
		else if (document.selection && document.selection.createRange)
		{
		var range = document.selection.createRange();
		var str = range.text;
		} 
		else
		{
			var str = "error";
		}
		textbox.style.fontFamily = 'Arial';
	textbox.insert_text(str, false);
	}
	// -->
	
</script>
</head>
<body>
<textarea id="textbox">HI</textarea>
<label style="color: red; font-family: Arial; font-weight: bold;">Some text to select</label>
<a href="javascript:wog_qq()">link</a>
</body>
</html>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 26157678
try the following code

This will give you an idea on how you can fetch the style of the containing element in the document where the selection is going to be made.

for example, it is give 2 alerts. first one will show the selected text. Second one will give the color of the selected text.

So you can easily make out the style of the container element and store it, so that when you paste the selected text you can also consider changing the style as per the source style.

Let me know if this is helpful

document.onmouseup = function(e) 
{
   var evt = window.event || e;
   if(window.getSelection)
   {
	t = window.getSelection();
   }
   else if(document.getSelection)
   {
	t = document.getSelection();
   }
   else if(document.selection)
   {
	t = document.selection.createRange().text;
   }		
   if (t.length > 0) 
   {
	alert(t);
   }
  alert ( evt.srcElement.style.color);
}

Open in new window

0
 

Author Closing Comment

by:WoG
ID: 31671467
Thanks for all ;)
0
 

Author Comment

by:WoG
ID: 26159394
Ok.
Above code working, but it give me style information about style all of element.

Example:
<div style="font: red;">
      text<span style="color: green;">green</span>texttexttext
</div>

If I select text "green" > alert will be "red".
How to get style from selected text?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question