I am trying to call a function from a link or button like the example below, however I would like to call the function whilst passing a variable along (not a string).

I was wondering if this was even possible, if not what is another solution?

This isn't my code, it is a simple way of explaining what I am trying to do.

<html> 
<head> 
<script type="text/javascript"> 
function myfunction(txt){ 
    alert(txt) 
} 
</script> 
</head> 

<body> 
<form> 
<input type="button" onclick="myfunction('Java!')" value="Java"> 

<input type="button" onclick="myfunction('Java too!')" value="Java too"> 
</form> 

</body> 
</html>

So instead of 'java too!' I would have a variable named "category" which could contain different data each time the button is pressed.

Hope this makes sense.

Any help would be kindly appreciated.

Cheers

Danny

I suspect that it took longer to come to this forum and post your question than it would have taken to test some of the alternatives yourself. And you no doubt would have had an answer sooner if you had - horrors of horrors - checked an online tutorial.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">

    var theQuestion = "What do you get if you multiply six by nine?"
    function myfunction(txt){ 
	    alert(txt) 
    } 

    </script>
    <title></title>
  </head>
  <body>
    <form>
      <input type="button" onclick="myfunction('javascript!')" value="click"> 
	<input type="button" onclick="myfunction(theQuestion)" value="click"> 
	<input type="button" onclick="myfunction(42)" value="click">
    </form>
  </body>
</html>

Right... well thanks for your help. I'm still trying to get use to the syntax of JavaScript. I had tried this and had failed results. I think it might be because of declaring the variables without the var, which I have found out to be global variables.

Anyways, thankyou for your reply, it has at least answered my question and removed one possibility from my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.