Simple JS Function Question
- Started
- Last post
- 16 Responses
- esko
I am trying to write a function and am new to all of this.
What is the best way to get my argument value to work throughout my function?
My function call: (it is part of an onChange event in a textarea):
------------------------------->
myFunction("SE11")My function:
------------------------------->
function myFunction(jobID) {
var myVariable = (document.all.periodpercentcompl...
alert(myVariable);
}I've also tried this:
function myFunction(jobID) {
var myVariable = eval("document.all.periodpercent... + jobID + ".value");
alert(myVariable);
}Any help on how to correctly, and efficiently write the argument into that statement would be really appreciated.
- mitsu0
well, i'm not sure i know what you're question is, but here's a simple approach:
just put this in the tag:
onChange='myFunction(this)'
and this in your script header
function myFunction(obj)
{
var myVar=periodpercentcomplete.valu...
alert(myVar)
}
- esko0
the problem is that this recordset holds like 600 records and they all have their own jobID, and the naming system sucks and doesn't make any sense, so when I loop through I just attach the jobID to the end of the names of the fields.
Basically, I have to grab a number from one field so I can calculate some info on another and then stuff it all into the database without leaving the page.
So I need to have that jobID inside the function so that I can specify the right fields to grab and calculate etc.
If you know of a different (read:better) way to do this im all ears but for now this is what's working.
If there is anyway to turn the jobID that is sent to the function into a variable inside the function that would probably work too.
- mitsu0
oh, i do this all the time... simple:
when you iterate through your recordset, just put the id in the function like this:
(not going to write the whole tag here - just the event handler)
response.write("onchange='myFunc... & rs.fields("id").value & ")'")
this is done in asp, but i'm sure you can get it to work with your ss-technology
- esko0
ok, so there is one other part. Each record has some other things attached to it.
It's like this - each job has activities (the particular job I am testing on has about 600), and then each activity could have a changeorder supplied which would change the price of an activity, and it's description, etc.. The actual purpose of this page is to scroll through and type in the percentage of the activity that is completed, do some calculations, grab some data and submit it to the database as soon as the user is finished inputting the information required of them.
So, the activityID (jobID in my other posts) is probably the most important piece of information that I have (or that I know how to use anyway).
And unfortunately there is no naming convention for these activityID's some start with SE, some start with SI, some dont have letters at all, etc.
- mitsu0
ok, are the activities linked to the jobid from another table?
it's hard to understand this w/out seeing a visual example.
i'll try to help ya out more when i get home in 30 min., or maybe someone else here can take a stab.
- esko0
yes the activities are linked to the jobID.
For this example I am making use of these tables.
Billings (tracks progress)
Activities
ChangeRequestsBillings holds information about how much of the project is finished. So whenever a user updates this page I am making it so that the progress for each Activity is tracked in the billings table.
Activities holds all of the information about the activity, like the description, start date, end date, how much it's going to cost, how much it gets billed.
ChangeRequests holds info about any changes that were made to an activity, this can make the cost of an activity go up and down but that is calculated on the page I am asking about, they do not directly affect the field in the activities table for the cost of the activity.
So this page is allowing the user to put in the percent complete for a given month, and it will grab that information without reloading the page etc., using an invisible iframe that updates the database when they click out of the text box for inputting the percent complete.
I grab some information out of the fields, do some calculations and store that, and grab some more info from some hidden fields and send it to the update page. Which creates a new record in the billings table with that days date input also.
- mitsu0
ok, back.
so what is it exactly that you are trying to figure out?
sounds like you have 3 tables linked to a job table or something...
do you have a screenshot/ mockup you can show me so i can better understand your question?
also, what are you writing this in? ASP, PHP, etc?
- esko0
I'm home now and don't have any of the code with me. I'll bump this thread tomorrow.
the job is specified by a field in the activities table.
asp, vbscript
- mitsu0
k
- esko0
ok, so I set this up on a free asp host.
here is the page.
http://www33.brinkster.com/neilh…here is the script.
http://www33.brinkster.com/neilh…I got the function thing figured out, if i want to do it the way that I was trying I would have to get ahold of that argument value like this.
function myFunction(jobID) {
var jobIdCollection = document.getElementsByName('peri... + jobID);
var jobIdValue = jobIdCollection[0].value;
I bumped the thread because I was hoping if you had a bit of spare time you could take a look at it and tell me if I'm doing this right. I have a feeling it could be coded 10x better/efficient.
- esko0
oh, and for that date page you can put in a date or just hit submit, doesn't really matter yet.
- mitsu0
looks like it's working to me... then agian, i'm not exactly sure what all it's supposed to do.
- esko0
Well, it's kind of working, it doesn't update yet, but that won't be too much of a problem. I was just wondering if you could take a glance at the code and tell me if it could've been done 10x easier.
- mitsu0
personally, i write my code so that when i come back to it to revise it later on, it makes sense to me... and it really pays off, cause i always wind up making revisions at some point.
if it does what you want it to do, it's fine. your code wasn't messy so that's also good. good job.
- esko0
thanks.
- xpedious0
wow neil, you got good at coding quick. congrats you obviously are doing well with your new job.