How do I pass variables into a JavaScript function?
-
Is it possible to pass variables into a js function? See code: function checkStartPrice (){ if ($('#StartingPrice')[0].value.length == 0){ alert("The 'Starting Price' cannot be left empty!"); return false; } else { var BuyItNowPrice = parseFloat($('#BuyItNowPrice').val()); var StartingPrice = parseFloat($('#StartingPrice').val()); var Reserve = parseFloat($('#Reserve').val()); if((BuyItNowPrice <= StartingPrice) && (StartingPrice > 0)){ alert("The 'Buy It Now' price must be higher..."); return false; } if((Reserve <= StartingPrice) && (StartingPrice > 0)){ alert("Your 'Reserve Price' must be higher..."); return false; } return true; } } I'd like to take the var BuyItNowPrice = parseFloat($('#BuyItNowPrice').val()); etc and place them outside the function, then call them in. The reason is I have 3 of these functions so these 3 variables (BuyItNowPrice, StartingPrice, Reserve) are repeated 9 times, 3 in each function. Is there a way of doing this? Any help would be Greatly Appreciated, Thanks
-
Answer:
A simple example of passing variables into a function is below. When you declare a function, you can also declare some arguments for the function by putting them inside the function() brackets. You can use whatever letter/name you like to represent the arguments - when you CALL the function, you pass through the variables. // put arguments into the function declaration - a,b,c function myfunction(a, b, c) { // use these arguments within the function return (a+b+c); } // declare the variables outside the function var first_variable = 1; var second_variable = 2; var third_variable = 3; // returns '6' myfunction(first_variable, second_variable, third_variable);
Nasir at Stack Overflow Visit the source
Other answers
You can make these variables global by taking it out of the fn BuyItNowPrice = parseFloat($('#BuyItNowPrice').val()); StartingPrice = parseFloat($('#StartingPrice').val()); Reserve = parseFloat($('#Reserve').val()); fn1(){} fn2(){} fn3(){} Now you can use these variables in all the three functions.
Ammu
Make those three variables global. To make them global you just need to have those variables outside any function. In javascript if any variable which doesnot appear in any functions, then its scope is global. To answer your first question, Yes it is possible to pass variables to functions in javascript. you can just send the variables as arguments to the function. sample: function myfucn(a, b) { //you implementation here.}
Anji
Related Q & A:
- How do I add custom attributes to javascript tags in Wordpress?Best solution by WordPress
- How can I transfer video from a dvr box to a computer?Best solution by Yahoo! Answers
- How can I print wirelessly from a laptop to a printer without a router?Best solution by answers.yahoo.com
- How do I go about getting a job working in a recording studio?Best solution by Yahoo! Answers
- How can I record directly from a keyboard onto a computer?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.