ojoonline
01-06-2007, 10:29 AM
Hi,
I found this perfect script in this forum and need to know whether it can be executed by a button outside the form tag.
Currently, it inserts at the cursor in a textarea when a button in the same form is clicked.
<script type="text/javascript">
function insert(el,ins) {
if (el.setSelectionRange){
el.value = el.value.substring(0,el.selectionStart) + ins + el.value.substring(el.selectionStart,el.selectionEnd) + el.value.substring(el.selectionEnd,el.value.length);
}
else if (document.selection && document.selection.createRange) {
el.focus();
var range = document.selection.createRange();
range.text = ins + range.text;
}
}
</script>
<form>
<input type="button" value="hello" onclick="insert(this.form.ta,'hello')">
<input type="button" value="my freind" onclick="insert(this.form.ta,'my friend')">
<br />
<textarea rows="7" cols="30" name="ta">
This is sample text, click anywhere in here then
choose on of the buttons above to see text inserted.
</textarea>
</form>
I have been using this code below where the button can be outside the form tags but always puts the inserted text at the end - which is not what I need. I need the button to put the text wherever the cursor is.
<script>
function inser1() {
document.getElementById('ip').value+= '<firstName>'
}
</script>
<input id="ins1" type="button" value="Add first name" class="personalisedbutton" onClick="inser1()" name="button">
<form>
<textarea id="ip" name="MessageText" class="halfFormMod"></textarea>
</form>
Any help greatly appreciated :-)
thanks,
jo
I found this perfect script in this forum and need to know whether it can be executed by a button outside the form tag.
Currently, it inserts at the cursor in a textarea when a button in the same form is clicked.
<script type="text/javascript">
function insert(el,ins) {
if (el.setSelectionRange){
el.value = el.value.substring(0,el.selectionStart) + ins + el.value.substring(el.selectionStart,el.selectionEnd) + el.value.substring(el.selectionEnd,el.value.length);
}
else if (document.selection && document.selection.createRange) {
el.focus();
var range = document.selection.createRange();
range.text = ins + range.text;
}
}
</script>
<form>
<input type="button" value="hello" onclick="insert(this.form.ta,'hello')">
<input type="button" value="my freind" onclick="insert(this.form.ta,'my friend')">
<br />
<textarea rows="7" cols="30" name="ta">
This is sample text, click anywhere in here then
choose on of the buttons above to see text inserted.
</textarea>
</form>
I have been using this code below where the button can be outside the form tags but always puts the inserted text at the end - which is not what I need. I need the button to put the text wherever the cursor is.
<script>
function inser1() {
document.getElementById('ip').value+= '<firstName>'
}
</script>
<input id="ins1" type="button" value="Add first name" class="personalisedbutton" onClick="inser1()" name="button">
<form>
<textarea id="ip" name="MessageText" class="halfFormMod"></textarea>
</form>
Any help greatly appreciated :-)
thanks,
jo