Sunday, March 25, 2012

Jquery, coffeescript and the javascript .html() working, and then not working

I ran into this problem where I made a form that had some jquery/javascript niceness to it that load up different forms when the users selects different links.  These forms have javascript/jquery of their own and all the code was there but it wasn't firing? WTF?

Well, it makes some sense because when the jquery code is initially run the form it's supposed to bind to isn't there.

So what now?

Answer, .live(), coffescript:

$ -> $("#my_id").live 'change', ->
if(this.checked)
$("#other_id").show()
else
$("#other_id").hide()


This makes the jquery goodness bind to all current and future instances with the given id.  Now the form that is loaded by user interaction has it's javascript functioning.


No comments:

Post a Comment