469,610 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

dynamic formname/fieldname

Hello,

I was wondering if I could do the following : can I change the formname and
fieldname with an argument? This way I can re-use the script multiple times.

My code (that doesn't work):

-----------------
<script>
function test(arg,arg2) {
document.arg.arg2.value="yes";
}
</script>

<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="javascript:test('frmtest','test1');">
</form>
-----------------

How can I make this work?
Thanks in advance!
Jul 23 '05 #1
6 1472
This should work :o)

<script type="text/javascript">
function test(arg,arg2) {
document.forms[arg][arg2].value="yes";
}
</script>

Best Regards

Nick (grunken.dk)

P.S. Sorry for doubleposts, I have never used Usenet before :o)

"geradeaus" <ge***********@hotmail.com> wrote in message
news:p9********************@hestia.telenet-ops.be...
Hello,

I was wondering if I could do the following : can I change the formname and fieldname with an argument? This way I can re-use the script multiple times.
My code (that doesn't work):

-----------------
<script>
function test(arg,arg2) {
document.arg.arg2.value="yes";
}
</script>

<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="javascript:test('frmtest','test1');">
</form>
-----------------

How can I make this work?
Thanks in advance!

Jul 23 '05 #2
geradeaus wrote:
Hello,
I was wondering if I could do the following : can I change the formname and
fieldname with an argument? This way I can re-use the script multiple times. <script>
function test(arg,arg2) {
document.arg.arg2.value="yes";
}
</script>

<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="javascript:test('frmtest','test1');">
</form>


Try this approach:
- make sure you declare the type of script correctly.
- arg will always have to be a form
- arg2 will always have to be an element of the arg form

<script type="text/javascript">
function test(arg,arg2) {
document.forms[arg].elements[arg2].value="yes";
}
</script>

Mike

Jul 23 '05 #3
On Wed, 21 Apr 2004 12:56:20 +0200, Grunken <gr*******@hotmail.com> wrote:
This should work :o)

<script type="text/javascript">
function test(arg,arg2) {
document.forms[arg][arg2].value="yes";
}
</script>
The more usual way is

document.forms[ arg ].elements[ arg2 ].value = ...;

though yours certainly works on my browsers.
P.S. Sorry for doubleposts, I have never used Usenet before :o)


A quick tip: don't top-post and quote verbatim. The preferred way to post
is to remove parts of the post (usually indicated with [snip] or similar)
that don't apply to your response, and reply under the remaining sections
so readers can understand exactly what you're answering.

This is especially important if the poster has made several points: one
long reply might be difficult to associate with each destinctive issue.

Welcome to Usenet, :)
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #4
On Wed, 21 Apr 2004 10:37:09 GMT, geradeaus <ge***********@hotmail.com>
wrote:
I was wondering if I could do the following : can I change the formname
and fieldname with an argument? This way I can re-use the script
multiple times.

My code (that doesn't work):
In addition to what others have said...
-----------------
<script>
function test(arg,arg2) {
document.arg.arg2.value="yes";
}
</script>

<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="javascript:test('frmtest','test1');">
The "javascript:" above only means something in Internet Explorer. In
other browsers, it is simply a label. You can remove it.
</form>
-----------------

How can I make this work?


You can apply a slight simplification. In intrinsic event handlers, such
as onclick, the this operator refers to the element that contains the
handler. For example,

<button type="button" value="Hello"
onclick="alert(this.value)">Test</button>

clicking the button above will display, "Hello".

Form controls have a property, form, that contains a reference to their
containing FORM element. You can use the technique above to simply getting
the form reference:

<script type="text/javascript">
function test( form, control ) {
form.elements[ control ].value = "yes";
}
</script>
...
<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="test(this.form,'test1');">
</form>

Good luck,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #5

"Michael Winter" <M.******@blueyonder.co.invalid> schreef in bericht
news:op**************@news-text.blueyonder.co.uk...
On Wed, 21 Apr 2004 10:37:09 GMT, geradeaus <ge***********@hotmail.com>
wrote:
I was wondering if I could do the following : can I change the formname
and fieldname with an argument? This way I can re-use the script
multiple times.

My code (that doesn't work):


In addition to what others have said...
-----------------
<script>
function test(arg,arg2) {
document.arg.arg2.value="yes";
}
</script>

<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="javascript:test('frmtest','test1');">


The "javascript:" above only means something in Internet Explorer. In
other browsers, it is simply a label. You can remove it.
</form>
-----------------

How can I make this work?


You can apply a slight simplification. In intrinsic event handlers, such
as onclick, the this operator refers to the element that contains the
handler. For example,

<button type="button" value="Hello"
onclick="alert(this.value)">Test</button>

clicking the button above will display, "Hello".

Form controls have a property, form, that contains a reference to their
containing FORM element. You can use the technique above to simply getting
the form reference:

<script type="text/javascript">
function test( form, control ) {
form.elements[ control ].value = "yes";
}
</script>
...
<form name="frmtest">
<input type="text" name="test1" value="hello">
<input type="button" onclick="test(this.form,'test1');">
</form>

Good luck,
Mike

--


Thanks for the advice!
Jul 23 '05 #6
Thanks Mike :o)

Your reply was very helpfull, and I will remember you'r advice !

I have used different forums for a long time, but never usenet because it's
a lot easyer to help peoble with scripts, who can write the way I do
(danish) :o)

Now I have to upgrade my english writing to the next stage, thats why I
selected USENET :o)

So if you can't get the meaning of what I'm writing - I know why :o)

Regards

Nick
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Wed, 21 Apr 2004 12:56:20 +0200, Grunken <gr*******@hotmail.com> wrote:
This should work :o)

<script type="text/javascript">
function test(arg,arg2) {
document.forms[arg][arg2].value="yes";
}
</script>


The more usual way is

document.forms[ arg ].elements[ arg2 ].value = ...;

though yours certainly works on my browsers.
P.S. Sorry for doubleposts, I have never used Usenet before :o)


A quick tip: don't top-post and quote verbatim. The preferred way to post
is to remove parts of the post (usually indicated with [snip] or similar)
that don't apply to your response, and reply under the remaining sections
so readers can understand exactly what you're answering.

This is especially important if the poster has made several points: one
long reply might be difficult to associate with each destinctive issue.

Welcome to Usenet, :)
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Pasquale | last post: by
2 posts views Thread by absolut | last post: by
2 posts views Thread by Luis Arvayo | last post: by
1 post views Thread by None | last post: by
reply views Thread by snehsoni | last post: by
reply views Thread by dawnyankee via AccessMonster.com | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.