1

How would you do something like this:

somemethod($("#buttonelement"));

function(ele) {
  ele.click(function(event) { alert("hi"); });
}

in other words how do you pass in an element as a jquery object and register the click event.

2
  • What's not working about the code you posted? Commented Apr 14, 2009 at 15:57
  • the function doesn't have a name. Commented Apr 14, 2009 at 15:57

1 Answer 1

10

Just like that...?

function someMethod(ele) {
    ele.click(function(event) {
        alert("hi");
    });
}

someMethod($("#buttonelement"));

The jQuery object is just like any other object in Javascript and can be passed to functions as normal.

Sign up to request clarification or add additional context in comments.

2 Comments

Must agree with Green, does not work! works with straight javascript tho: var obj = document.getElementById('downpmt'); ..then send obj to your function.
@HankCastello @ Green: if the above code "sometimes" does not work for you then you definitely have a problem somewhere else in your code. Claiming that the above function doesn't work "sometimes", and therefore isn't right, is voodoo programming.

Your Answer

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