i have the following code which extends the JQuery and adds a method to the JQuery:
$.fn.attachWithMessage = function () {
$(this).focusin(showMessage());
}
function showMessage() {
alert('hi');
}
so I can use that code as follows :
<input type="text" name="name" id="textbox" />
$(document).ready(function () {
$("#textbox").attachWithMessage ();
});
when I load the page for the first time, a message box shows up with (‘hi’) message.
even if I didn’t click in the text box.
I also tried the click event, and the message still shows automatically.
any ideas ??
The issue here is that when you pass
showMessage()as a parameter tofocusin, the functionshowMessageis executed and the return value is passed tofocusin.Instead you need to pass a reference to the function (without the paranthesis).
Use the following code to extend:
Working example@ http://jsfiddle.net/eXEP5/
EDIT:
If you want to pass a parameter to showMessage then try this: