View Full Version : how can I set onclick to a function with parameters

03-25-2005, 09:15 PM
I am using an initialization function that reads an XML document to create the HTML document. One part of my HTML document should look like this.

<a class="parentTopic" href="#" onClick="changeState(5)" onDblClick="changeState(6)">
<span class="topicText">Course Dates</span><span class="topicArrow">4</span>
<div class="outline">
<div class="outlineTopic">Course Launch Date/Next Course </div>
<div class="outlineTopic">Book Shipment Date/Next Book Shipment Date</div>
<div class="outlineTopic">Shipping Blackout Period</div>

note the onClick event has a function with a parameter attached to it. But in JS i know you can do [element].onClick = [function], but doing that doesn't allow you to send in a parameter. How on earth do you set the onclick event to a function with a parameter?

Willy Duitt
03-25-2005, 10:00 PM
Depends where the parameter is coming from and what it is...

One way would be (passing an object reference):
element.onclick = new Function("function(this)");

Another would be (passing a parameter with quotes):
element.onclick = new Function("function('"+parameter+"')");

Passing a variable without quotes:
element.onclick = new Function("function("+parameter+")");