PDA

View Full Version : Include text in focused input field onsubmit button


Sirius
11-12-2002, 12:23 PM
First of all, I great creators of this forum, I`m sorry for my English, Its my first post and I`m try search this question in the search!
Sorry for small offtopic...
Now about problem:

I have this script:

function insert(text)
{document.forms[0].mesg.focus();
document.forms[0].mesg.value = document.forms[0].mesg.value + text;
}

<a href="javascript:insert(some)">some</a>

This script insert text into input field with name "mesg".
But I need that script will insert text to not only mesg input field, and also in all (when focused) inputs in form.

Is it possible?

beetle
11-12-2002, 03:37 PM
You could make a 2nd parameterfunction insert(text, elem) {
var e = document.forms[0].elements[elem];
e.focus();
e.value += text;
}

<a href="java script:insert('some','mesg')">some</a> Will that work for you?

Sirius
11-12-2002, 06:55 PM
Thanks for reply, but you don`t understand me!

I`m need to create simple keyboard with feature write on focused fields or textareas.
Because may be several field and textarea and I can`t create button for each field :)

I`m understand your variant, but element "elem" must be current focused field. How i can do it?

beetle
11-12-2002, 07:05 PM
Well, first of all, a field does not have to have focus for you to manipulate it's value with javascript.

I'm still not sure that I understand. You need a link that will add text to several form fields at once?

Sirius
11-12-2002, 09:51 PM
No :)

Example I have link:
<a href="javascript:insert(some)">some</a>

And three input fields:
<input type=text name=name1>
<input type=text name=name2>
<input type=text name=name3>



When i set my cursor on first input (cursor is blink on input) my link by submit include text only in first input.
When i go to second input (by TAB or mouse) my link working (include text by submit) only in second input.
When i go to third input my link working only in third input.

Its simple as i think, or no? :)

Roy Sinclair
11-12-2002, 10:07 PM
Not quite so simple. The problem is that when you click on the link the focus is removed from the form field and moved to the link instead. The code running from the link therefore has no way to know which field previously had the focus (or even if any field ever had the focus).

In order to make this work you'll need a global variable to keep track of which form field had the focus. Then your code can use that global variable to insert the text to the right form field.


var hadFocus = null

function insert(text)
{
if (hadFocus == null) return false;
hadFocus.value = hadFocus.value + text;
return false;
}

...

<a href="javascript:insert('some')">some</a>

...

<input type="text" name="name1" onfocus="hadFocus = this; return true;">
<input type="text" name="name2" onfocus="hadFocus = this; return true;">
<input type="text" name="name3" onfocus="hadFocus = this; return true;">

Sirius
11-13-2002, 05:17 AM
Thanks...
I think that I`m solve this problem with your help! I`m some change in last code, comment return false in insert func - thanks, this all that I`m needed:


<script>
var hadFocus = null

function insert(text)
{
if (hadFocus == null) return false;
hadFocus.value = hadFocus.value + text;
//return true;
}
</script>

<form>
<a href="javascript:insert('some')">some</a>
<input type="text" name="name1" onfocus="hadFocus = this; return true;">
<input type="text" name="name2" onfocus="hadFocus = this; return true;">
<input type="text" name="name3" onfocus="hadFocus = this; return true;">
</form>