glennn.php Posted July 14, 2010 Share Posted July 14, 2010 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 ) GN Quote Link to comment Share on other sites More sharing options...
premiso Posted July 14, 2010 Share Posted July 14, 2010 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() { } Quote Link to comment Share on other sites More sharing options...
glennn.php Posted July 14, 2010 Author Share Posted July 14, 2010 that's what i read, using "var" and NOT using "var", only neither worked... Quote Link to comment Share on other sites More sharing options...
premiso Posted July 14, 2010 Share Posted July 14, 2010 If you are using firebug, check your logs. I am not so sure that getElementById('name').value is a valid parameter for that object. Quote Link to comment Share on other sites More sharing options...
glennn.php Posted July 14, 2010 Author Share Posted July 14, 2010 well, the function works great with them inside it - i'm not a javascript coder whatsoever. i work in php. in other words, what's firebug? ) Quote Link to comment Share on other sites More sharing options...
Adam Posted July 14, 2010 Share Posted July 14, 2010 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. Quote Link to comment Share on other sites More sharing options...
glennn.php Posted July 14, 2010 Author Share Posted July 14, 2010 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 Quote Link to comment Share on other sites More sharing options...
premiso Posted July 15, 2010 Share Posted July 15, 2010 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. Quote Link to comment Share on other sites More sharing options...
Adam Posted July 15, 2010 Share Posted July 15, 2010 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(); } Quote Link to comment Share on other sites More sharing options...
glennn.php Posted July 15, 2010 Author Share Posted July 15, 2010 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... Quote Link to comment Share on other sites More sharing options...
glennn.php Posted July 15, 2010 Author Share Posted July 15, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.