PDA

View Full Version : Passing JS Variables From HTML to a .JS file



DankaShine
19 Jul 2005, 08:46 PM
How does one do it???

I want the user to click on a link and, depending on the variable content, the image on the page will change. Basically, how do I pass the variable that I declare in my link to a JS file which then shows the appropriate image?

Umm... this is the code I have, maybe it'll show you what I'm trying to do:



<a href="#" onClick="javascript: goForth('var projNum=1;');">


It's obviously wrong, but do you see where I'm going with it? goForth is a function that is defined in my .js file.

Thanks for any help.

Rincewind
21 Jul 2005, 05:32 AM
When passing a variable to a function, you pass the value of the variable, not the definition. For example:

onClick="javascript: goForth(1);"

Would send the value 1 to the function such as:

function goForth(projNum)
{
some statements
}

In this case the variable projNum would take on the value of 1.

You can list several variable values and names in the function call. Just separate them by a , comma.