Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combining Functions with Parameters?

    I have 3 functions in which 75% of their statements are the same. Can I combine them into 1 function using parameters? If so, how do I call a parameter from a link and define them in the function?


    I believe the function and links that call it would look something like this:

    function One(WhatDoIPutHere) {

    all common statements here;

    if (Link1) {
    do stuff;
    }

    if (Link2) {
    do stuff;
    }

    if (Link3) {
    do stuff;
    }
    }

    <a href="javascript:void(0);" onmousedown="One('Link1')">Link1</a>

    <a href="javascript:void(0);" onmousedown="One('Link2')">Link2</a>

    <a href="javascript:void(0);" onmousedown="One('Link3')">Link3</a>


    Thanks for your help.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Here is one way you can do it:

    function One(num) {

    all common statements here;

    if (num=='Link1') {
    do stuff;
    }

    if (num=='Link2') {
    do stuff;
    }

    if (num=='Link3') {
    do stuff;
    }
    }

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks John.

    I understand what == does but what is "num"? Is it an abreviation for number?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    num was just an arbitrary variable name I choose. In my mind it did mean number but it really could be anything you want. Choose a vairbale name that makes sense to you. The variable name will represent the parameter that you passed in. It could just as easily been:


    function One(fredtheVariableIamIam) {

    all common statements here;

    if (fredtheVariableIamIam=='Link1') {
    do stuff;
    }

    if (fredtheVariableIamIam=='Link2') {
    do stuff;
    }

    if (fredtheVariableIamIam=='Link3') {
    do stuff;
    }
    }
    Last edited by JohnKrutsch; 06-19-2002 at 05:42 AM.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL thanks John.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •