Jump to content

global vars help


glennn.php

Recommended Posts

i don't know how to make certain vars global, or at least available to several functions:



function mult1()
{
// Get values from form

mo = document.getElementById('monthly1').value;
oft = document.getElementById('often1').value;

// Compute the product.
var product = mo*oft;

// Display product in form.
// document.wizForm.sc1_net.value = product;
document.getElementById('net1').value = product;

n1 = document.getElementById('net1').value;
n2 = document.getElementById('net2').value;
n3 = document.getElementById('net3').value;
n4 = document.getElementById('net4').value;
n5 = document.getElementById('net5').value;
n6 = document.getElementById('net6').value;
n7 = document.getElementById('net7').value;
n8 = document.getElementById('net8').value;
n9 = document.getElementById('net9').value;
n10 = document.getElementById('net10').value;

tot = parseInt(n1) + parseInt(n2) + parseInt(n3) + parseInt(n4) + parseInt(n5) + parseInt(n6) + parseInt(n7) + parseInt(n8) + parseInt(n9) + parseInt(n10);
document.getElementById('total').value = tot;
}


 

i'd like to make n1 through n10 available to 10 functions just like this one, but i've not been able to. i've tried what i've found online, but to no avail.

 

i'd REALLY like to make this function work for ten different forms, and requested help in another string, but have received no replies...

 

thanks for your help, in either manner :o)

 

GN

 

Link to comment
Share on other sites

I am not a javascript expert, but putting them outside of the function "should" make them global IE:

 

var n1 = document.getElementById('net1').value;
var n2 = document.getElementById('net2').value;
var n3 = document.getElementById('net3').value;
var n4 = document.getElementById('net4').value;
var n5 = document.getElementById('net5').value;
var n6 = document.getElementById('net6').value;
var n7 = document.getElementById('net7').value;
var n8 = document.getElementById('net8').value;
var n9 = document.getElementById('net9').value;
var n10 = document.getElementById('net10').value;

function one() {
     // processing here with n1 etc. 
}

function two() {

}

Link to comment
Share on other sites

JavaScript is an event driven language. You need to declare the variable globally, but define it within a function (triggered by an event):

 

var n1;
var n2;
// ...

 

Then in the function:

 

n1 = document.getElementById('net1').value;
n2 = document.getElementById('net2').value;
// ...

 

 

Note "var" is used globally, but not within the function.

Link to comment
Share on other sites

JavaScript is an event driven language. You need to declare the variable globally, but define it within a function (triggered by an event):

 

var n1;
var n2;
// ...

 

Then in the function:

 

n1 = document.getElementById('net1').value;
n2 = document.getElementById('net2').value;
// ...

 

 

Note "var" is used globally, but not within the function.

 

so i'm still going to have to define these 10 vars in each of these 10 functions anyway, which is what i was trying to avoid...

 

then isn't there a way to make this function more generic in order to do the same thing for 10 different forms, like

 

function mult()
{
// Get values from form

mo = document.getElementById('monthly...').value;
oft = document.getElementById('often...').value;


var product = mo*oft;


document.getElementById('net...').value = product;

 

where there are a series of form fields, id="monthly1", id="monthly2", etc...? where the function is called from a form select onchange event...?

 

maybe not...?

 

thanks much, MrAdam

Link to comment
Share on other sites

There sure is, using an switch statement and passing a parameter to the function

 

function multi(which) {
     switch (which) {
             case 1:
                       // case 1 code here
                     break;
             case 2: 
                      // case 2 code here
                     break;
     }
}

 

Should treat you right. Since I do not know how you call this method, here is an example call using a href.

 

<a href="javascript:multi(1);">Multi - 1</a>

Will send you to the case 1 statement.

Link to comment
Share on other sites

JavaScript is an event driven language. You need to declare the variable globally, but define it within a function (triggered by an event):

 

var n1;
var n2;
// ...

 

Then in the function:

 

n1 = document.getElementById('net1').value;
n2 = document.getElementById('net2').value;
// ...

 

 

Note "var" is used globally, but not within the function.

 

so i'm still going to have to define these 10 vars in each of these 10 functions anyway, which is what i was trying to avoid...

 

No you don't. Once you've declared the variables globally you can define them within one function, and then access them (or define them again) within another.

 

Probably better explained with a simple example:

 

var foo; // notice the "var" statement

function test1()
{
    foo = 'bar'; // notice no "var" statement
}

function test2()
{
    alert(foo);
}

window.onload = function() {
    test1();
    test2();
}

Link to comment
Share on other sites

There sure is, using an switch statement and passing a parameter to the function

 

function multi(which) {
     switch (which) {
             case 1:
                       // case 1 code here
                     break;
             case 2: 
                      // case 2 code here
                     break;
     }
}

 

Should treat you right. Since I do not know how you call this method, here is an example call using a href.

 

<a href="javascript:multi(1);">Multi - 1</a>

Will send you to the case 1 statement.

 

awesome, Premiso, and i thank you, but it's a (simple mathematic) javascript function i'm calling:

 

function mult()
{
// Get values from form

mo = document.getElementById('monthly...').value;
oft = document.getElementById('often...').value;


var product = mo*oft;


document.getElementById('net...').value = product;
}

 

where i don't know how to get the three form field variables passed ...

 

<input id= "monthly1", "monthly2", etc...

<input id= "often1", "often2", etc...

<input id= "net1", "net2", etc...

Link to comment
Share on other sites

hell, i'm sorry, P. - i didn't realize switch() comes in javascript, too... my bad...

 

thanks!

 

There sure is, using an switch statement and passing a parameter to the function

 

function multi(which) {
     switch (which) {
             case 1:
                       // case 1 code here
                     break;
             case 2: 
                      // case 2 code here
                     break;
     }
}

 

Should treat you right. Since I do not know how you call this method, here is an example call using a href.

 

<a href="javascript:multi(1);">Multi - 1</a>

Will send you to the case 1 statement.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.