SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast Michael-from-Earth's Avatar
    Join Date
    Apr 2001
    Location
    Springfield, IL
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript function passing arguments.

    What I want is a loop that will call the same function until it reaches a certain number (let's say 10). Then I want the function to take the parameter passed (the number) and use it in the function where ever you see a 1 below. Basically I want it to run on each element of the pub_name array and from the info gathered define a variable based on the number (example: display_pub_name_*). I have run some tests, but I'm a little rusty and I must have the systax messed up. Any help is appreciated.

    -Michael

    check_name_length();

    function check_name_length ()
    {
    if (pub_name[1].length > 30)
    {display_pub_name_1 = pub_name[1].substring(0,27) + "<b>...</b>";}
    else
    {display_pub_name_1 = pub_name[1];}
    }

  2. #2
    SitePoint Enthusiast Michael-from-Earth's Avatar
    Join Date
    Apr 2001
    Location
    Springfield, IL
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here to post my own answer (once again )!

    for (i = 1; i <= limit; i++)
    {check_name_length(i);}

    function check_name_length (num){
    if (pub_name[num].length > 30)
    {display_pub_name[num] = pub_name[num].substring(0,27) + "<b>...</b>";}
    else
    {display_pub_name[num] = pub_name[num];}
    }

    Hopefully someone else will see this and be helped by it.

    Later,
    Michael

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks good. one question though, i think that arrays with javascript are zero based. because you set i=1 to start your loop, you are actually missing the first element in your array which is located at the array[0] position.

  4. #4
    SitePoint Enthusiast Michael-from-Earth's Avatar
    Join Date
    Apr 2001
    Location
    Springfield, IL
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point... but I never declare the [0] element . It's just undefined. I have multiple scripts working together (perl to build .js files, .js to serve content, asp (vbscript) to process content) and it is easier to start everything at 1 so I can keep track of how the scripts interact (besides, changing it to be zero based now would be a HUGE job ). I *usually* remember to compensate for it when performing operations that are based on the number of elements in an array... usually!

    Regards,
    Michael


Bookmarks

Posting Permissions

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