passing arguments to a function assigned to event attributes

A

Andrew Falanga

Hi,

Well, I'm learning slow but sure. I'm having a different problem
now. Basically, I have a function (my form validation function, that
I want to be able to abstract into a library. That means, I want the
function to work on any form it's called on. The function will take
two arguments. The first is the form object itself and the second is
a comma separated list of names that may be NULL of all the input
elements in the form.

What I'm having trouble with is the syntax of the html to associate
this function to the onsubmit attribute of the form. This is what
I've got:

<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
comments,husband_lastName,husband_firstName)" action=<?PHP echo
($_SERVER['PHP_SELF'] . "?new=insert"); ?> method=post>

Which obviously isn't going to work because each of those commas will
be interpreted as separate arguments to the function. I put it
quotes, but then ended up with an HTML syntax error when the page is
loaded (thanks to whomever it was here that informed me of the "Error
Console" in Firefox, man, that's just awesome). So, how do I pass
this comma separated list of values into this function?

Thanks,
Andy
 
D

David Mark

Hi,

Well, I'm learning slow but sure.  I'm having a different problem
now.  Basically, I have a function (my form validation function, that
I want to be able to abstract into a library.  That means, I want the

Why not concentrate on the basics first. No need to worry about a
library until you have at least one re-usable function under your
belt.
function to work on any form it's called on.  The function will take

Why such a generalized approach for a first function? No matter what
you come up with, there are likely forms that will break it.
two arguments.  The first is the form object itself and the second is
a comma separated list of names that may be NULL of all the input
elements in the form.

I don't know what that means.
What I'm having trouble with is the syntax of the html to associate
this function to the onsubmit attribute of the form.  This is what
I've got:

<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
comments,husband_lastName,husband_firstName)" action=<?PHP echo
($_SERVER['PHP_SELF'] . "?new=insert"); ?> method=post>

Don't post PHP code here. You want people to take the time to read
your sample right? And where did all of those variables come from (or
are those supposed to be string literals?)
Which obviously isn't going to work because each of those commas will
Obviously.

be interpreted as separate arguments to the function.  I put it

I think I understand what you are saying (even if you don't.)
quotes, but then ended up with an HTML syntax error when the page is
loaded (thanks to whomever it was here that informed me of the "Error
Console" in Firefox, man, that's just awesome).  So, how do I pass
this comma separated list of values into this function?

Something like this:

<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
['comments', 'husband_lastName', 'husband_firstName'])"
action="someservergeneratedlocation" method="post">

Don't just copy and paste that blindly. You need to understand what
you are doing. Start by learning the basics of the language. And
forget writing a library until you can write (and understand) a simple
function.
 
A

Andrew Falanga

Why not concentrate on the basics first.  No need to worry about a
library until you have at least one re-usable function under your
belt.

At this point, when I say library, all I'm talking about is taking
this one function and putting into a single file that several
different html files may refer to. At this point, all I want the
function to do is to check that all elements of a form that must be
filled in, are; and skip all others.
I don't know what that means.

Poor wording on my part, sorry.
What I'm having trouble with is the syntax of the html to associate
this function to the onsubmit attribute of the form.  This is what
I've got:
<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
comments,husband_lastName,husband_firstName)" action=<?PHP echo
($_SERVER['PHP_SELF'] . "?new=insert"); ?> method=post>

Don't post PHP code here.  You want people to take the time to read
your sample right?  And where did all of those variables come from (or
are those supposed to be string literals?)

I'll remember that in the future, thank you for educating me. I
didn't realize that it was taboo.
I think I understand what you are saying (even if you don't.)

How is it that I don't understand what I'm saying? I simply want to
know how to pass a comma separated string to the function as a single
argument.
quotes, but then ended up with an HTML syntax error when the page is
loaded (thanks to whomever it was here that informed me of the "Error
Console" in Firefox, man, that's just awesome).  So, how do I pass
this comma separated list of values into this function?

Something like this:

<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
 ['comments', 'husband_lastName', 'husband_firstName'])"
action="someservergeneratedlocation" method="post">

Don't just copy and paste that blindly.  You need to understand what
you are doing.  Start by learning the basics of the language.  And
forget writing a library until you can write (and understand) a simple
function.

I can see that I need to invest in a good book on Javascript. Please,
any suggestions? Good authors?

Thanks,
Andy
 
D

David Mark

At this point, when I say library, all I'm talking about is taking
this one function and putting into a single file that several
different html files may refer to.  At this point, all I want the
function to do is to check that all elements of a form that must be
filled in, are; and skip all others.

Okay. That's not a library per se.
I don't know what that means.

Poor wording on my part, sorry.


What I'm having trouble with is the syntax of the html to associate
this function to the onsubmit attribute of the form.  This is what
I've got:
<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
comments,husband_lastName,husband_firstName)" action=<?PHP echo
($_SERVER['PHP_SELF'] . "?new=insert"); ?> method=post>
Don't post PHP code here.  You want people to take the time to read
your sample right?  And where did all of those variables come from (or
are those supposed to be string literals?)

I'll remember that in the future, thank you for educating me.  I
didn't realize that it was taboo.


I think I understand what you are saying (even if you don't.)

How is it that I don't understand what I'm saying?  I simply want to
know how to pass a comma separated string to the function as a single
argument.




Something like this:
<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
 ['comments', 'husband_lastName', 'husband_firstName'])"
action="someservergeneratedlocation" method="post">
Don't just copy and paste that blindly.  You need to understand what
you are doing.  Start by learning the basics of the language.  And
forget writing a library until you can write (and understand) a simple
function.

I can see that I need to invest in a good book on Javascript.  Please,
any suggestions?  Good authors?

Well, there's the rub. Virtually every book on Javascript is bunk.
See the FAQ and the accompanying notes.
 
A

Andrew Falanga

Well, there's the rub.  Virtually every book on Javascript is bunk.
See the FAQ and the accompanying notes.

That's not very encouraging. To which FAQ are you referring? I did,
"javascript FAQ," at google and was returned many hits. However, your
statement makes me think that you are specifically referring to one
thing in particular. Where would I find that one?

Andy
 
D

David Mark

That's not very encouraging.

No it isn't. That is one of the reasons why so many developers resort
to rearranging code, rather than programming.
To which FAQ are you referring? I did,

The group FAQ.
"javascript FAQ," at google and was returned many hits.  However, your
statement makes me think that you are specifically referring to one
thing in particular.  Where would I find that one?

Search the group instead. The FAQ link is posted daily AFAIK.
 
T

Thomas 'PointedEars' Lahn

Andrew said:
That's not very encouraging. To which FAQ are you referring? I did,
"javascript FAQ," at google and was returned many hits.

It's the sixth Google hit, here and now. The one miraculously titled --
surprise! -- "comp.lang.javascript FAQ". The one being referred to in
almost every other posting, including the daily postings.
However, your statement makes me think that you are specifically
referring to one thing in particular. Where would I find that one?

You don't seem to have read a while here before posting, which is a bad
thing to begin with. Nobody wants to read the same old questions and solve
the same old problems all over again. Really. That is why there is a FAQ.

You have been warned.


PointedEars
 
T

Trevor Lawrence

Andrew,
Well, I could end up as persona non grata with c.l.j. but you can send a
list of comma separated values as a parameter to a function. They are
actually passed as an object so that the format is {name1 : value1 , name2:
value2 , .... }

An example follows.

<html>
<head>
<script>
var amap = 'dajsdkl' ;
var acenter = 'sdljksd' ;
var aTitle1 = 'A name of a map' ;
var aTitle2 = 'A street address' ;

function setMarker(map, center, opts) {
alert('map: ' + map) ; // displays 'map: dajsdkl'
alert('center: ' + center) ; // displays 'center: sdljksd'
alert ('rTitle1: ' + opts.rTitle1) ; // displays 'rTitle1: A name of a map'
alert ('rTitle2: ' +opts.rTitle2) ; // displays 'rTitle2: A street
address'
}
</script>

</head>
<body onload = "setMarker(amap, acenter, {rTitle1: aTitle1, rTitle2:
aTitle2} );">
</body>
</html>

This shows two parameters (map and center) passed as values and the last
(opts) passed as an object, which itself contains two name:value pairs. Of
course, onload= can be onclick = or onsubmit = etc. inside the relevant HTML
tag

Can you take it from here?

--
Trevor Lawrence
Canberra
Web Site http://trevorl.mvps.org

Why not concentrate on the basics first. No need to worry about a
library until you have at least one re-usable function under your
belt.

At this point, when I say library, all I'm talking about is taking
this one function and putting into a single file that several
different html files may refer to. At this point, all I want the
function to do is to check that all elements of a form that must be
filled in, are; and skip all others.
I don't know what that means.

Poor wording on my part, sorry.
What I'm having trouble with is the syntax of the html to associate
this function to the onsubmit attribute of the form. This is what
I've got:
<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
comments,husband_lastName,husband_firstName)" action=<?PHP echo
($_SERVER['PHP_SELF'] . "?new=insert"); ?> method=post>

Don't post PHP code here. You want people to take the time to read
your sample right? And where did all of those variables come from (or
are those supposed to be string literals?)

I'll remember that in the future, thank you for educating me. I
didn't realize that it was taboo.
I think I understand what you are saying (even if you don't.)

How is it that I don't understand what I'm saying? I simply want to
know how to pass a comma separated string to the function as a single
argument.
quotes, but then ended up with an HTML syntax error when the page is
loaded (thanks to whomever it was here that informed me of the "Error
Console" in Firefox, man, that's just awesome). So, how do I pass
this comma separated list of values into this function?

Something like this:

<form id="NewPatientForm" onsubmit="return CheckFormValues(this,
['comments', 'husband_lastName', 'husband_firstName'])"
action="someservergeneratedlocation" method="post">

Don't just copy and paste that blindly. You need to understand what
you are doing. Start by learning the basics of the language. And
forget writing a library until you can write (and understand) a simple
function.

I can see that I need to invest in a good book on Javascript. Please,
any suggestions? Good authors?

Thanks,
Andy
 
D

David Mark

Andrew,
Well, I could end up as persona non grata with c.l.j. but you can send a

I imagine you will hear about the top-posting and quoted signatures.
list of comma separated values as a parameter to a function. They are
actually passed as an object so that the format is {name1 : value1 , name2:
value2 ,  ....  }

Now how is that equivalent to a comma-separated string? Perhaps you
meant an array?

[snip invalid example]
 
T

Trevor Lawrence

Andrew,
Well, I could end up as persona non grata with c.l.j. but you can send a

I imagine you will hear about the top-posting and quoted signatures.
list of comma separated values as a parameter to a function. They are
actually passed as an object so that the format is {name1 : value1 ,
name2:
value2 , .... }

Now how is that equivalent to a comma-separated string? Perhaps you
meant an array?

[snip invalid example]

No, strictly speaking, it is not a list of comma separated values. Of course
an array could be passed, but passing a object has some advantages
1. Variables/parameters (whatever you want to call them) can be retrieved by
name, rather than by position
2. There can be zero, one or many variables
3. Each call can pass variables of completely different names and types
(although the function has to detect this.)
4. The OP has another option to try.
 

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,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top