0
function addValidEvent(item,event,func)
{
 if (item.addEventListener) 
 {
 item.addEventListener(event, func, false); 
 }
 else 
 if (item.attachEvent) 
 {
 item.attachEvent("on" + event, func);
 } else {
 //todo
 }
}

I call it like

addValidEvent(element, "mouseover", inRadio(element));

inRadio this is other function, I need to register this with out call a inRadio in addValidEvent call.

How to correct pass the function like param ?

asked Feb 14, 2012 at 15:25
1

4 Answers 4

4

pass it as function. Now you are calling method inRadio. Do it like this

addValidEvent(element, "mouseover", function() {inRadio(element); });
answered Feb 14, 2012 at 15:27
Sign up to request clarification or add additional context in comments.

Comments

3

The context on which your function is called is the element itself, so you could pass the function reference(name) as the event handler:

addValidEvent(element, "mouseover", inRadio);

or you can wrap your function call in an anonymous function(this might come in handy when you want to have more control over the callback) :

addValidEvent(element, "mouseover", function(){inRadio(element);});
answered Feb 14, 2012 at 15:27

Comments

2

In your code you are actually calling the function and the return value of that call is being passed as thrid argumnent to addValidEvent method.

Try this.

addValidEvent(element, "mouseover", inRadio);

As a side note: Since jQuery is tagged to this question I would suggest you to use jQuery for this which is so simple.

$(element).mouseover(inRadio);
answered Feb 14, 2012 at 15:27

Comments

-1
var a = function() {
 // code here
}

Then do what you need to with 'a'

But I prefer the previous answers :)

answered Feb 14, 2012 at 15:27

3 Comments

How is this helpful? I fail to see a relation to the question.
I'd still say this is an rather incomplete example. It is not clear how a integrates with the existing code.
I did not say that ;) Anyways, I understand the problem, I just want to encourage you to improve your answer. All you show is how to create a function, which I suppose to OP already knows.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.