Forum Moderators: open

Message Too Old, No Replies

Passing form name

         

thaedge

8:52 pm on Aug 23, 2004 (gmt 0)

10+ Year Member



Its been awhile since Ive done JS and I'm having a bit of a brain fart and am unable of finding a solution via searching so thought I'd ask.

Form name is "foo" and below the form (or could be before) I need to call function "readfoo()". How do I reference the form name when calling the function? Reason I ask this is I will have common JS calling dynamic forms - sometimes only once per page, sometimes more. Thus I need to pass the form name or else they all explode.

So

-- Form Foo --

-- call to readfoo(reference form); --

readfoo(myformnamevariable){

somevariable = myformnamevariable.formfield.length;

--

I'm sure its something very simple that I have overlooked, but like I said, Im having a major brain fart on this.

Thanks in advance to anyone with any ideas

Rambo Tribble

10:36 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you need to address a form name dynamically, use associative array notation, as in:

document.forms["foo"]

A string variable can replace the literal string in the example. This technique can be extended to named elements as in:

document.forms["foo"].elements["fubar"]

Bernard Marx

11:05 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm rather in favour of this form myself:

window["document"]["forms"]["foo"]["elements"]["fubar"]

;]

thaedge

2:57 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



That is assuming I know the Form name in my function. The problem I have is passing a form name to a function.

Thanks for the ideas, but you are assuming I know the name of the form when I reference it, and I only will have a variable - that is if I ever get it to work.

Rambo Tribble

3:22 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please note where I said that a variable could replace the literal, above.

Oh, and Bernard, I think you may have been associating with your arrays a bit overmuch. To be on a first name basis with all of them, after all.

StupidScript

5:33 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From within an element of the form:

i.e. <input type="submit" onclick="readfoo(this.form)">

to pass the form name to the script, then:

function readfoo(frm) {
somevar=frm.elements["somefield"].value;
}

referencing the variable passed to readfoo(). Pass additional comma-separated references to "catch" and use in the function:

<select name="list" onchange="readfoo(this.form,this,this.options[this.selectedIndex])">

and:

function readfoo(frm,fld,idx) {
someformname=frm.name;
somefieldname=fld.name;
somevarvalue=idx.value;
}

What kind of situation exists where you do not know the name of the form you wish to use?

Bernard: Do you really use that expression of the array? I've never seen it done like that, before! You continue to surprise... :)

thaedge

6:02 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



Thanks Stupid Script, only I need the function call to happen independent of an action on the form.

runfunction(formname);


runfunction(myform){

myform.variable.value........

Like I said at the start(but stated poorly), the point is to pass a form name to a function so that I can have 1, 2 or 3 forms call the same JS and can reuse and better update it in the future.

Bernard Marx

8:35 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not quite sure whether you feel you've got your solution now or not. So apologies if I'm just going over old ground.

Rambo gave the solution in his first reply, and in fact Stupid's version (sending a reference) is equally applicable to your request:

>> so that I can have 1, 2 or 3 forms call the same JS and can reuse and better update it in the future

Stupid's is arguably better, because you can use exactly the same statement everytime. (Rambo would have done this too, I reckon, if you hadn't been explicit about using the form

name
).

Here's what you're after.

HTML 

<form name="one">
<input type="button" onclick="readfoo('one')">
...
<form name="two">
<input type="button" onclick="readfoo('two')">
...
<form name="tre">
<input type="button" onclick="readfoo('tre')">

SCRIPT

function readfoo(formRef)
{
var form = document.forms[formRef]
// var form = document.forms[formRef]¦¦ formRef //* alt (#1)
...
}

The key thing (as I er.. underlined earlier) is that these terms are exact synonyms:

object.propName
object['propName']

(tre is three in Swedish BTW. It keeps things looking neat.
fem, sex, sju are also useful for this.)

I'm feeling big today, so I'll go one better than both these honourable members.If you use the commented-out line instead, it becomes able to take both forms of referencing (#2) - object ref, or name string. So you can use the function calls above, and / or the universal:

<input type="button" onclick="readfoo(this.form)">
(#3)

//

#1 - amend ¦¦ to unbroken pipes.
#2 - just as long as you don't give your forms the unlikely names of

"[object]"
or
"HTMLFormElement"

#3 - the input element must be inside a form, of course.

[edited by: Bernard_Marx at 9:02 pm (utc) on Aug. 24, 2004]

Bernard Marx

8:58 pm on Aug 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First name basis, Rambo? The whole scene man, except for the window that is.

Of course, you can only address the window indirectly, via one of it's self-referential properties. Just because one of them is named

window
, it thinks we're fooled. I'm certainly not, and what's more, it's not the kind of behaviour I look for in an associate to be on string terms with.

Rambo Tribble

1:50 am on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid I prefer to treat them all as mere objects, myself. Their lives are so fleeting, their state so changeable; I hate becoming attached.

thaedge

2:53 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



Thanks guys but you are missing a big part of my problem.

I need the call to the function to happen not on "onclick" but on page load. So it can not be connected to a "onsubmit" or "onclick" action.

Rambo Tribble

3:40 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The onload event of the body or the window can be polled and your function executed subsequent to the event being realized. This does beg the question of how you intend to feed the variables to the function. An example:

<body onload="yourFunction('str_var_1','str_var_2');">

or:

<script type="text/javascript">
window.onload=yourFunction(x,y,z){
//function code
}
</script>

StupidScript

5:12 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want to make sure the form has initialized, before calling it. Often, the BODY's onload event happens before forms on the page are fully initialized, due to variances in browser JS handling, among other things.

So, I submit THIS for your inspection:

<script>
function readfrm(frm,tmout) {
clearTimeout(tmout);
var thaform=frm;
... etc ...
}
</script>

<form name="one" onload="doone=setTimeout('readfrm(this,\"doone\")',200)">
</form>

<form name="two" onload="dotwo=setTimeout('readfrm(this,\"dotwo\")',400)">
</form>

<form name="tre" onload="dotre=setTimeout('readfrm(this,\"dotre\")',600)">
</form>

... etc ...

The forms are initialized, then call the function (which kills the appropriate setTimeout) after a few microseconds' delay ... staggered, so they don't "collide" during execution of the function.

Eh? Are we getting closer to the solution? :)

(I didn't test this, but my only nagging doubt is how I included the setTimeout reference for clearing ... I think I escaped the delimeters correctly ... Bernard?)

The previous examples in this thread by our esteemed colleagues are chock full of good theory on this.

Bernard Marx

6:32 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Often, the BODY's onload event happens before forms on the page are fully initialized, due to variances in browser JS handling

Now, I didn't know that. Do you think that that applies to the Javascript assignment,

window.onload
, too? (more subtle differences to tease out there).

I think I escaped the delimeters correctly ... Bernard?

Yes, that escaping carries all the hallmarks of a true professional; the delimiters delimit sublimely.
...it's this that is putting us all to shame.

What?

this, that's what.

setTimeout executes the statement string in a global context, so

this
refers to the window.
Now, go back and do it again.

Rambo Tribble

10:43 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't believe the onload event is commonly supported by the form element. Some browsers support onload on images or other tags that load external content, but the standard elements that support the event are the window, body, and frame elements.

StupidScript

11:53 pm on Aug 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, Bernard. <hanging head>

I had thought that "this" referred to the current object (form, image, etc.)? The setTimeout is carrying the child "readfrm()". Does that not move the burden of evaluating "this" to "readfrm()", ergo setting its value to its container (the <form> object)?

How about this? (Assuming the forms are consistently initialized prior to the BODY onload event):

<script>
function readfrm(frm,tmout) {
clearTimeout(tmout);
var thaform=frm;
... etc ...
}
function initForms() {
for(i=0;i<=document.forms.length-1;i++) {
tmo="do"+i;
tmo=tmo.toString();
tmlen=200*i;
tmo=setTimeout('readfrm(document.forms[i],tmo)',tmlen);
}
</script>

<body onload="initForms()">

<form name="one"></form>
<form name="two"></form>
<form name="tre"></form>

... more forms of varying quantity ...

</body>

Please can I go play outside, now? :)

Bernard Marx

12:45 am on Aug 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll have to rip all of that to shreds in the morning. It's far too late at night for me to do it the justice it so richly deserves. I think you will need your sense of humour at hand.

..and yes, you can, but don't be playing with those rough kids (like Tribble).

thaedge

8:59 pm on Aug 27, 2004 (gmt 0)

10+ Year Member



I knew I was just having a brain fart this week. To many people asking me too many things and not able to focus on this.

Took a good look at it today, and used what Bernard and Rambo said originally.

<form>

code

</form>

<script language="JavaScript">

var myform = 'formname';

myfunction();

</script>

Within the function(s) rather then constantly passing the variable I set it above and not pass it to the 20 subfunctions that need the reference. Within the subfunctions I reference the form via document.forms[myform] and that solved my problem. Im sure there is a better way to do the reference, but Im just happy to hack with some JS and get it to work :)

Thanks to all those that spent time helping me. I need to stop coding CF one day and look at JS beyond simple form verification again.

Bernard Marx

10:38 pm on Aug 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's cool. If it works, the it certainly works. Nobody's programming spaceships, and a global var can take you as far as you need to go.