Click to See Complete Forum and Search --> : Knowing what the user selects


ollmorris
01-04-2003, 02:19 AM
I want the user to be able to block of (or select) some text in a text area and then I can write it into a prompt(); command. I have tried this function but it wouldn't work:


function getRange(){
var selection = form.content.selection.createRange();
return selection;
}


Is this the wrong function or am I doing something wrong?

Thanks

swon
01-04-2003, 02:30 AM
Hi ollmorris, try this one:

function getRange(){
var selection = document.selection.createRange();
var val = selection.text;
alert(val);
}

ollmorris
01-04-2003, 03:38 AM
Hi Swon.

Thanks but I can't seem to get it too work. My code is below:


function getRange(){
var selection=document.selection.createRange();
var val=selection.text;

if(Action==1){
txt=prompt("Enter text to be BOLD",+val);
if(txt!=null)
AddTxt=""+txt+"";
}
}

swon
01-04-2003, 04:06 AM
txt=prompt("Enter text to be BOLD",+val);

I think it should be val not +val

ollmorris
01-04-2003, 04:14 AM
Now when I do that my script does nothing. Nothing pops up with the code:


function getRange(){
var selection=document.selection.createRange();
var val=selection.text;

if(Action==1){
txt=prompt("Enter text to be BOLD", val);
if(txt!=null)
AddTxt=""+txt+"";
}
}

swon
01-04-2003, 08:53 AM
This works for me:

<html>
<head>
<script language="JavaScript">
function getRange(Action){
var selection=document.selection.createRange();
var val=selection.text;

if(Action==1){
txt=prompt("Enter text to be BOLD",val);
if(txt!=null)
AddTxt=""+txt+"";
}
}
</script>
</head>
<body>
<a href=# onClick="getRange('1')">bold</a><br>
<textarea name="text" cols="" rows=""></textarea>
</body>
</html>

ollmorris
01-04-2003, 09:32 AM
how could I get it specially for the textarea?

ollmorris
01-04-2003, 11:52 AM
Hi dave

I used


var objTxtRange = objTxt.selection;
alert("This is your selection: '"+objTxtRange.text+"'");
objTxtRange.text = (objTxtRange.text.charAt(objTxtRange.text.length - 1) == ' ') ? text + ' ' : text;


but I get an error. What am I meant to change? Oh and so I learn something could you explain it please. Thanks you so much

ollmorris
01-04-2003, 02:14 PM
I still can't get it too work. Here is my code for the required elements:


<html><head>
<script language="JavaScript" type="text/javascript">
function storeCaret(objTxt){
if (objTxt.createTextRange){
objTxt.selection = document.selection.createRange().duplicate();}}

function insertAtCaret (objTxt, content) {
if (objTxt.createTextRange && objTxt.selection) {
var objTxtRange = objTxt.selection;
objTxtRange.content = (objTxtRange.content.charAt(objTxtRange.content.length - 1) == ' ') ? content + ' ' : content;
} else { objTxt.value += content;}}

function AddText(form, Action){
var AddTxt="";
var txt="";

if(Action==1){
txt=prompt("Enter text to be *bold*","");
if(txt!=null)
AddTxt+=txt;
}
form.content.value+=AddTxt;
}
</script>
</head><body><form action="" name="edit"><input name="bold" type="button" class="form" value=" B " onClick="AddText(this.form,1);insertAtCaret(this.form.content,this.form.content.value);"><textarea name="content" cols="60" rows="8" class="form"onSelect="storeCaret(this);" onClick="storeCaret(this);" onKeyup="storeCaret(this);"></textarea>
</form></body></html>


Thanks and can you also tell me how to have the inital value of the prompt, the text the user has selected.

I appreciate your help so much

ollmorris
01-06-2003, 03:01 AM
thanks dave!