...

View Full Version : javascript newbie in need of help with my function



michaelnyden
02-12-2007, 06:21 PM
so I am working on my company intranet site and made a function:
<script language="javascript">
<!-- Begin
var j;
var a="designer";
var b="accountant";
var c="consultant";
function description(j) {
if (j > 0){
form.job.value = a;
}
if (j > 1){
form.job.value = b;
}
if (j > 2){
form.job.value = c;
}
}
End -->
</script>

I already know since this is my first function, there are major issues with it, what I want to do is a form with checkboxes is on this webpage I made, when you select the textboxes, I want to pass the value to the above function, and have the function export to a hidden form called "job" which will be saved out to a file which already works fine, but the output is blank which leads me to believe either the function is wrong, or the value from the checkboxes are not passing to the function or it's not outputting to the hidden field "job" as it's value...
here is the code for the check boxes:
<input type="checkbox" name="job1" value="1"><b>CAD
design/drafting</b>
<p><input type="checkbox" name="job2" value="2"><b>Accounting</b></p>
<p><input type="checkbox" name="job3" value="3"><b>Consulting</b></p>

and here is the code for the hidden field:
<input type=hidden name=job onFocus="this.blur()">

and lastly, here is the code which should pass the value of the checkboxes upon clicking submit (ignore the other functions that it submits through, they work just great...):
<input type="submit" value="Submit" name="SubmitNetworkRecord" tabindex="100" onFocus="changeCase(this.form.firstname); changeCase(this.form.lastname)" onClick="javascript:makeUserID(this.form, this.form.firstname.value,
this.form.lastname.value); description(this.form.job1.value); description(this.form.job2.value); description(this.form.job3.value)">

MikeFoster
02-12-2007, 07:47 PM
Hi Michael, Welcome to Coding Forums!

A few suggestions:

1. IMO, using html comments in script elements is not needed any more. Also note that that line is not a Js comment so the "Begin" and "End" comments you have there are going to cause Js errors.

2. I suggest not using "form" as the "name" of a form.

3. I suggest a more standard way of accessing form element objects. For example if the form has name="form1" then access it like this:


var f = document.forms['form1'];

if the form has id='form1' then access it like this:


var f = document.getElementById('form1');

michaelnyden
02-12-2007, 10:22 PM
thank you for your suggestions, but I am still very lost/confused...so are the variables from those check boxes being passed to the function? or is it failing afterwards, such as passing the output from the function to the value of the hidden form field..?

MikeFoster
02-12-2007, 11:17 PM
I saw several things I wanted to point out - but I'm headed out the door and it was quicker to just type the following. I may not have chosen the best ways of doing things. Compare it to your code and ask any questions you want.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Template</title>
<script type='text/javascript'>
window.onload = function()
{
var f = document.forms['form1'];
f.onsubmit = form1OnSubmit;
f.SubmitNetworkRecord.onfocus = snrOnFocus;
}
var jobs = {"1":"designer", "2":"accountant", "3":"consultant"};
function description(j) {
var desc = jobs[j];
return desc || "invalid job number";
}
function form1OnSubmit()
{
// makeUserID(this, this.firstname.value, this.lastname.value);
var j;
if (this.job1.checked) {
j = description(this.job1.value);
alert(j);///////
}
if (this.job2.checked) {
j = description(this.job2.value);
alert(j);///////
}
if (this.job3.checked) {
j = description(this.job3.value);
alert(j);///////
}
this.job.value = j;
// return false;
}
function snrOnFocus()
{
// changeCase(this.form.firstname);
// changeCase(this.form.lastname)"
}
</script>
</head>
<body>

<form name='form1'>
<input type="checkbox" name="job1" value="1"><b>CAD design/drafting</b>
<p><input type="checkbox" name="job2" value="2"><b>Accounting</b></p>
<p><input type="checkbox" name="job3" value="3"><b>Consulting</b></p>
<input type=hidden name=job onFocus="this.blur()">
<input type="submit" value="Submit" name="SubmitNetworkRecord" tabindex="100">
</form>

</body>
</html>

tonyp12
02-13-2007, 12:18 AM
if (j > 0){form.job.value = a}
if (j > 1){form.job.value = b}
if (j > 2){form.job.value = c}

If J=3 All the 'if' sentances will be true and
it will do them all, should still be OK as the is will stop on the last that was true.

But that should still be avoided.

michaelnyden
02-13-2007, 05:18 PM
mikefoster, you are a god! thank you ever so much...works perfectly!

-Michael

MikeFoster
02-13-2007, 07:28 PM
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum