dynamic formname/fieldname

G

geradeaus

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>
 
G

Grunken

This should work :eek:)

<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 :eek:)
 
M

Michael Winter

This should work :eek:)

<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 :eek:)

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
 
M

Michael Winter

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.

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
 
G

geradeaus

Michael Winter said:
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.

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!
 
G

Grunken

Thanks Mike :eek:)

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) :eek:)

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

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

Regards

Nick


Michael Winter said:
This should work :eek:)

<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 :eek:)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top