View Full Version : onclick: multiple textfields

02-01-2007, 06: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) {
var range = document.selection.createRange();
range.text = ins + range.text;

<form name="f">
<input type="button" value="a" onclick="insert(this.form.ta,'a')">
<input type="text" name="ta" />

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:

02-01-2007, 06:34 PM
How about checking if the text area is in onfocus or onblur?