I tried to get as much info into the title as posible... hope it doesnt confuse anyone

i have an initAll() called on window.onload

within initAll() I assign a few onclick handlers to some links. However, it would be great if I could pass an argument to the handler, so I could use the same handler every time... but if I include two brackets for each handler, it executes immediately... ignoring the 'onclick' bit...

window.onload = initAll;

function initAll(){
	document.getElementById('btn_events').onclick = getSpecificFeed('events');

function getSpecificFeed(type) {
	var url = make_abs('home','specific','feed=' + type); //special fn to create an absolute url for my ajaxCall fn to use
	var query_string = ''; //post vars for my ajax fn - not used this time
	ajaxCall(url,query_string,refreshFeed); //special ajaxcall fn which returns output to the refreshFeed fn below

//this fn catches http.responseText from ajaxCall
function refreshFeed(reply){
	document.getElementById('feed_items').innerHTML = reply;
7 Years
Discussion Span
Last Post by lifeworks

When you do:


you are basically calling the function.

The correct way will be:

htmlElement.onclick = functionName; // No braces

function functionName(event) {
    // Do stuff with event object.
    // Note: event object is passed automatically.

Solution for passing arguments to these function:-

htmlElement.onclick = function(e) {  myHandler(e, myparam);  }

function myHandler(event, param) {
    // Do stuff with event OR your param

Is this enough?

Edited by Luckychap: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.