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;
}

Recommended Answers

All 2 Replies

When you do:

functionName();

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?

aah, awesome, worked great! thanks!!

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.