0

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/

Ashfame
  • 1,731
  • 1
  • 25
  • 42

2 Answers2

5

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!

jasonmerino
  • 3,220
  • 1
  • 21
  • 38
  • Your fiddle uses jQuery and will throw an error if actually used with Zepto, `$.live()` is also deprecated and only `$.on()` should be used. – Torsten Walter Jul 18 '12 at 23:48
  • You're right! I was messing around with the code and forgot to change back to passing Zepto into the anonymous function. I've updated my answer. Now the page shouldn't even load jQuery at all. Thanks! – jasonmerino Jul 19 '12 at 00:03
1

The problem in your fiddle was that somehow fiddle screwed up the whole document write thing.

You were close though. You can't attach an event listener to a node that's not there which you tried with $(node).on().

However, from the linked documentation you are supposed to use it like so:

$(document).on("click", "selector", fn);

I've updated your fiddle to use Zepto instead of jQuery and also set it to run on domReady which makes the ready event in your code unnecessary.

http://jsfiddle.net/zR2xL/3/

Torsten Walter
  • 5,614
  • 23
  • 26