The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Apply Javascript to more than one table

I am using a Javascript script to apply alternating colors to table rows on a web page. The script is called by this line in the HTML:

<body onload="stripe('prices','#ebf1fa','#b4c7e6');">

('stripe' is the name of the JS function, and the parameters are the ID of the table I want the script to work on ('prices') and the colors that the alternating table rows are intended to be).

This actually works fine as it is, but I would like the same effect to also apply to another table on the same web page which has an ID of 'technical.' Please can anyone advise me how to achieve this - I'm assuming I just need to rewrite the line above in some way to also apply the same effect to the 'technical' table. I have tried different ways of rewriting the line, but with no success so far. Any help would be much appreciated!

There is a better way to do it though, which avoids the page loading delay that's inherent with the onload event. Don't use the onload event and run the script directly from the bottom of the body. This is one of the best practices for speeding up your web site.

It's useful to have the code in the function because the name of the function helps you to understand what's going on there, but it can be a nuisance to create a function that's used only once, and be called immediately after it's been defined.

We can use a self-invoking function to help solve those issues. Normally they're anonymous, but they don't have to be. The name of the function is helpful when reading the code to let you know what's going on.