Search code examples
zepto

Zepto's on implementation for Focus and Blur events


I need to run some code on focus and blur events on elements that were injected into the DOM after page load. So I am using Zepto's on (Zepto's on link) to run the code but it doesn't work for me.

Here is my jsfiddle in which I am trying to make it work - http://jsfiddle.net/ashfame/zR2xL/


Solution

  • Your on declaration was a little off in the original JSFiddle. When you use the .live() "version" of .on() you select the document with Zepto (because, I believe, that's what the .live() function does behind the scenes) then apply the .on() method and pass it the parameters event, selector, and function. It looks something like this:

    $(document).on(event, selector, function);
    

    Check out this JSFiddle that I modified a bit from the one you posted in the comments.

    The changes I made were:

    • rearrange the on function
    • commented out the jQuery test via console.log() to stop errors from being thrown
    • prevent the default of the click event on the anchor element
    • switched the document.write to a $('body').append()

    Hope that helps!