How to Pass an Integer to a Function in JavaScript

Passing values to functions is what makes JavaScript functions dynamic in your code. You pass an integer to a function, the function makes the calculations and a value returns. You use JavaScript functions to change values in form fields. JavaScript is client-side code, so it runs on the reader's browser after the dynamic code runs on the server and returns the Web page.

Instructions

    • 1

      Right-click the HTML file you want to edit and select "Open With." Double-click the HTML editor you want to use to edit the JavaScript code.

    • 2

      Type the JavaScript function. For instance, if you want to double a number and enter it into a form field value, the following "myFunction" code executes an integer calculation:

      <script type="text/javascript">

      function myFunction(integer_value)

      {

      document.getElementById("result").value = integer_value*2;

      }

    • 3

      Pass the integer value to the function. You trigger functions in buttons or other elements. Place the following code in a button or other element's properties to call the function and pass an integer:

      onClick="myFunction(1)"

      The code sends the integer "1" to the function.

    • 4

      Click the "File" menu item and click "Save" to save the changes.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured