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 .

Recommended Answers

All 16 Replies

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

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

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.

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

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.

Agree with Twiss, this is a help forum, not a do it for you forum

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

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

Post your code, first.

<!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>

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

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?

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 :)

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.