Gossamer Forum
Home : General : Internet Technologies :

Javascript multiple fields, same name!

Quote Reply
Javascript multiple fields, same name!
If I have say 5 form fields with the same name eg...

<input type="text" name="F1">
<input type="text" name="F1">
<input type="text" name="F1">
<input type="text" name="F1">
<input type="text" name="F1">

....how can I update the value of one of them with javascript?
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
I don't think you can. I could be wrong, but I don't think it's legal to have multiple fields with same name (will still compile, but can't access them).

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Javascript multiple fields, same name! In reply to
Do you mean it is illegal html?

You can access them in perl with:

my @field_values = $IN->param('F1');

Last edited by:

Paul: Apr 11, 2002, 10:33 AM
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
Try the script bellow:

function update(obj) {
for (var i=0;i<frm_name.elements.length;i++) {
var e = frm_name.elements;
if ((e.name == obj) && (e.type=='text')) {
e.value = "Test";
break;
}
}
}

Use it: update('F1');

TheStone.

B.
Quote Reply
Re: [TheStone] Javascript multiple fields, same name! In reply to
Hi,

Thanks for the reply. I couldn't get it to work though. No errors but nothing updated.

Let me explain what Im trying to do incase it becomes clearer....

Ok for my chat script I have a def editor built in to manage forms....you can see how it looks here:

http://www.wiredon.net/...at/mini_def_full.jpg

Using a JS prompt Im asking the administrator to enter a field name (Im listing some common regexs to make it easier for admin)....so I have a link like:

<a href="#" onclick="do_regex('^\w+$');">Letters, Numbers and Underscore</a>

Now Im trying to get the javascript to insert that regex into the regex field in the image above.

I just can seem to get it to work.

Last edited by:

Paul: Apr 11, 2002, 11:08 AM
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
You should change the subroutine a little bit:

function do_regex (obj, val) {
....
e.value = val;
...
}

And then you'll be able to use: <a href="javascript:do_regex('field_name','value')">Letters, Numbers and Underscore</a>

TheStone.

B.
Quote Reply
Re: [TheStone] Javascript multiple fields, same name! In reply to
But how does it know which field to update? ....won't it change all fields to ^\w+$ ? (remember all fields have the same name on each row.)

Thanks.
Quote Reply
Re: [TheStone] Javascript multiple fields, same name! In reply to
Oh also I forgot to ask, how do you update a field when the name is given from a prompt?

eg...the following doesn't work:

var field = prompt("Please enter a field to update:", "");

document.form_name.field.value = "XYZ";

Thanks for your help.

Edit: I just tried the code suggested in your last post but I still can't seem to get it to work :(

Last edited by:

Paul: Apr 11, 2002, 11:32 AM
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
Find the attached file, you will see how it works


TheStone.

B.
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
In Reply To:
var field = prompt("Please enter a field to update:", "");
document.form_name.field.value = "XYZ";


It should be:
var field = prompt("Please enter a field to update:", "");

var obj = eval('document.form_name' + field)
if (typeof(obj) == 'undefined') {
alert('Invalid field name');
return;
}
else {
obj.value = "XYZ";
}

TheStone.

B.
Quote Reply
Re: [TheStone] Javascript multiple fields, same name! In reply to
Thanks so much for your help. We are almost there, however it always alerts that the field is invalid.


Here's what I have:

Code:
<script language="javascript">
function update(val) {
var field = prompt("Please enter a field to update:", "");
var obj = eval('document.def' + field)
if (typeof(obj) == 'undefined') {
alert('Invalid field name!');
return;
}
else {

var count = 0;
for (var i=0;i<def.elements.length;i++) {
var e = def.elements;
if ((e.name == obj) && (e.type=='text')) {
count++;
if (count == 4 ) {
e.value = val;
}
}
}
}
}

</script>
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
Here the script:

Code:


<script>
function update(val) {
var field = prompt("Please enter a field to update:", "");
if (!field) { return; }
var count = 0;
for (var i=0;i<frm_name.elements.length;i++) {
var e = frm_name.elements;
if ((e.name == field) && (e.type=='text')) {
count++;
if (count == 4 ) { e.value = val; }
}
}
if (count == 0) { alert('Invalid field name') }
}

</script>

B.
Quote Reply
Re: [TheStone] Javascript multiple fields, same name! In reply to
Hehe, thanks again but that still gave the alert.
Quote Reply
Re: [Paul] Javascript multiple fields, same name! In reply to
Try the attached file again Crazy

Edit: you should pass in 'F1'

B.

Last edited by:

TheStone: Apr 11, 2002, 12:27 PM
Quote Reply
Re: [TheStone] Javascript multiple fields, same name! In reply to
Ahh it was the forum turning the code italic that hid the [i]

Thanks for your help and patience.