'Form event function triggers jquery function

I have a textbox form that when focused triggers a function, but the problem is this function was inside jQuery function and its not working, see my codes...

<input type="text" name="clickMe" onfocus="focusMe()" />

I want this text box triggers an event when focused and its the focusMe() function, yet I want this function use jQuery that's why I put it under jQuery function...

$(function(){

    focusMe(){
      //events happens here
    }

});

I know there are jQuery methods to make the focus event happens like #(element).focus() function... But I want it be triggered the event using form event like above onfocus="focusMe()" ... Anyone can tell me what should I do to the function under jQuery?



Solution 1:[1]

You do not need to wrap jQuery code in $(function(){ necessarily. Functions, for example, are best suited outside. Here's the code you'll use to make focusMe run:

var focusMe = function() {
    alert('running');
}

Solution 2:[2]

You don't want that function in the jQuery block.

You can just place it before it and it will be able to use jQuery functions.

Solution 3:[3]

Since you want it attached as an event, you should use jQuery's .focus() method. You would do it like this:

   <input type="text" id="clickme"/>


   (function() {
       $('#clickme').focus( function() {
           alert('focused');
       });
   });

Hope that helps. Good luck.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wanovak
Solution 2 PeeHaa
Solution 3 Jemaclus