SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery focus event problem

    I'm writing a plug-in for jquery and have hit a stumbling block
    Code:
    jQuery.fn.fullTextArea = function(options) {
    
    	if(opts.mode === 'focus') {
    		return this.each(function(){
    			this.focus(createOverlay());
    		});
    	} else if(opts.mode === 'button') {
                        do something else
    	}
    };
    
    function createOverlay() {
        function defined here
    }
    Even if the element the plugin is applied to doesn't have focus, createOverlay always runs on document load. I've tried putting createOverlay at various places in the source, but it always runs.

    It looks like the focus event is being triggered for some reason. How do I stop this happening?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this.focus(createOverlay);

    By including the parenthesis, you're calling the function, when you just want a reference to the function itself that you can pass to jquery. Well, unless createOverlay() just returns a function reference.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    by the time you'd replied I'd managed to find another solution: defining the function createOverlay() as a variable, but I think I prefer your solution.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rhysickle View Post
    by the time you'd replied I'd managed to find another solution: defining the function createOverlay() as a variable, but I think I prefer your solution.
    That's doing exactly the same thing.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •