0

hello evryone,

i need acode that returning my the selected text in iframe (by giving the ID of him)
i need that this code will work for FF, IE , SAFARI, GC and the active browsers,

thanks allot !
haim .

3
Contributors
16
Replies
17
Views
6 Years
Discussion Span
Last Post by twiss
0

Start with posting what you tried? Is that selected text in an input box or textarea or somewhere else?

0

i want to make BBcode (wysiwyg editor)
and i need to konow what is the selected text from the iframe that i create
this iframe is editable

0

Take a look at window.getSelection(). If you've got that to work, you might want to get IE <= 8 to work too, with window.selection.

-1

can you write for me code ?
please
that work at all browsers

1

Nope. Try yourself first, post what you tried, and what doesn't work or what you don't understand, but I'm not going to write this for you.

0

can you guys giv me some link that going to help me ?

0

ok, thanks
but thats no working on IE
someone can please send me link \ code that works on all browsres ?

0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="he" lang="he" dir="rtl">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" /> 
    <meta name="author" content="Haimz.net" />
	<script type="text/javascript">
	function getSelectedText()
	{
		var userSelection;
		if (window.getSelection) 
		{
			userSelection = window.getSelection();
		}
		else if (document.selection) 
		{
			userSelection = document.selection.createRange();
		}

		if(userSelection == "")
			userSelection = "you didnt select enything";
		else
			userSelection = "your selection: " + userSelection;
		
		alert(userSelection);
	}
	</script>
	<title>train</title>
</head>
<body>
	my text lalalla
	<button onclick="getSelectedText();">try</button>
</body>
</html>
0

If you read on, the clue is lower on the page: for the ms way, first get the text attribute.

0

oh thanks alot man !
thats my final code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="he" lang="he" dir="ltr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" /> 
	<script type="text/javascript">
	function getSelectedText()
	{
		var userSelection;
		if (window.getSelection) 
		{
			userSelection = window.getSelection();
		}
		else if (document.selection) 
		{
			userSelection = document.selection.createRange();
		}

		if (userSelection.text)
			userSelection = userSelection.text;
			
		if(userSelection == "" || userSelection == "[object TextRange]")
			userSelection = "Selection empty";
		else
			userSelection = "Your selection:\n " + userSelection;
		
		alert(userSelection);
	}
	</script>
	<title>Training</title>
</head>
<body>
	<p>
		Text text text text<br />
		Nice text ! :)
	</p>
	<button onclick="getSelectedText();">Selection try</button>
</body>
</html>

does its works for all browsers?

0

Why did you add userSelection == "[object TextRange]" ?
I consider things working in "all browsers" if they do in Firefox, Chrome, Opera, and Internet Explorer 6+, but you'll have to check that yourself :)

Edited by twiss: n/a

0

userSelection == "[object TextRange]"
Because check that for IE, on ie the object is not empty he has this value .

ok now im trying to get the positions of the start & end, (substring)
can you giv me alink if i'm not going to success

(sory for bad english)

0

Search before you ask, please, and stop asking for a link, there are search engines for that.
As another hint, throw the selection object at console.log() and look at the properties it has. That should help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.