...

View Full Version : Add text at a certain location inside an input



BarrMan
02-23-2007, 12:57 PM
Hey, I'd like to make a function which adds a certain text to a certain place inside my input. ie: this is my input:

["Hello() This is my input"]
and I'd like to insert "world" inside the parenthesis.
How can I do it?

And if I want to do the following without even knowing the place I want it to go, and just use the function to determine the place that was last focused on the input, how can I do that?

I know it's possible since I've used this code in the past but, unfortunetly, I can't find it and I've tried googling this but had no clue how this function is called.

Thanks for any help!

vwphillips
02-23-2007, 02:48 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--

function checkCursor(zxcid,zxctag){
var zxcobj=document.getElementById(zxcid)
var zxcbegin=0;
if (document.selection){
zxcobj.focus();
var zxcsel=document.selection.createRange();
if(zxcsel.text==''){
zxcsel.text='';
dummy=zxcobj.createTextRange();
dummy.findText('');
dummy.select();
zxcbegin=zxcobj.value.indexOf('');
document.selection.clear();
zxcAddTag(zxcobj,zxcbegin,zxcbegin,zxctag)
zxcobj.focus();
}
if(zxcsel.text!=''){
var zxclen=zxcsel.text.length
zxcsel.text=''+zxcsel.text;
zxcdummy=zxcobj.createTextRange();
zxcdummy.findText('');
zxcdummy.select();
zxcdummy.scrollIntoView();
zxcbegin=zxcobj.value.indexOf('');
document.selection.clear();
if(zxcbegin+1==zxcbegin+zxclen){ zxcAddTag(zxcobj,zxcbegin,zxcbegin,zxctag); }
else{ zxcAddTag(zxcobj,zxcbegin,zxcbegin+zxclen,zxctag); }
}
}
else if (zxcobj.selectionStart||zxcobj.selectionStart=='0') {
zxcbegin=zxcobj.selectionStart;
var zxcend=zxcobj.selectionEnd;
if(zxcend==zxcbegin){ zxcAddTag(zxcobj,(zxcbegin),zxcbegin,zxctag); }
if(zxcend!=zxcbegin){
if(zxcend==zxcbegin+1){ zxcAddTag(zxcobj,zxcbegin,zxcbegin,zxctag); }
else{ zxcAddTag(zxcobj,zxcbegin,zxcend,zxctag); }
}
}
}

function zxcAddTag(zxcobj,zxcsrt,zxcend,zxctag){
var zxcval=zxcobj.value;
zxctxt=zxcval.substring(zxcsrt,zxcend);
zxcsrt=zxcval.substring(0,zxcsrt);
zxcend=zxcval.substring(zxcend);
zxcobj.value=zxcsrt+zxctag+zxctxt+zxctag.replace('<','</')+zxcend;
}

//-->
</script>

</head>
<body>
<input id="txt" value="abcdefgdefghijklmno" ><br>
<input type="button" name="" value="<p> Tag" onclick="checkCursor('txt','<p>');">
</body>
</html>




var myvar='fred';
inputobj.value=inputobj.value.replace('()','('+myvar+')');

BarrMan
02-23-2007, 04:09 PM
Hey, Thanks for the reply!
The code doesn't work so good because it adds <p> twice (closing it as well in both cases) and I don't want it to surround the whole text, just the part that is selected.
Also, I wanted it to find the cursor location.
Btw, you don't have to write the whole code, just the functions (the javascript commands as in createTextRange) that I could use.

Thanks again philip!

BarrMan
02-26-2007, 04:37 PM
bump.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum