nashua
02-01-2007, 05:00 PM
I found a simple script which inserts a character in a given text field:
<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 name="f">
<input type="button" value="a" onclick="insert(this.form.ta,'a')">
<input type="text" name="ta" />
</form>
If I add another field, the script continues to insert in the text field labeled as "ta". Is it possible to change the script so that the character would be inserted where the cursor is and regardless the text field name. :confused:
<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 name="f">
<input type="button" value="a" onclick="insert(this.form.ta,'a')">
<input type="text" name="ta" />
</form>
If I add another field, the script continues to insert in the text field labeled as "ta". Is it possible to change the script so that the character would be inserted where the cursor is and regardless the text field name. :confused: